mangled error message when re-throwing exceptions

exception

#1

Consider this simple example:

function failme()
	error("failme() failed")
end

function test()
	try
		failme()
	catch err
		error("error in test(): $err")
	end
end

function main()
	try
		test()
	catch err
		println("error in main(): $err")
		quit()
	end
end

Calling main() returns the following (on Julia 0.6.2 on Debian):
error in main(): ErrorException("error in test(): ErrorException(\"failme() failed\")")
Note how the innermost part gets mangled by escaping the quotes (which should not be part of the string) - is this a bug and if so, should i report it somewhere?

A couple of notes/questions:

  • Is this a wrong way of using exceptions in Julia, or should it work? In the real code, the reason for catching the exception in the “middle layer” is that I can add more information/context to the error message.
  • I tried to replace error("info") with a custom exception (based on this code), but this gives the same result as long as I include $err in the message of the exception thrown in test()
  • Replacing $err with $(sprint(showerror, err)) helps, but looks weird
  • Replacing error() with throw() helps as well, but it feels wrong to throw strings … but might be the easiest solution

As usual, I am probably missing something obvious…


#2

Perhaps you are looking for rethrow?

help?> rethrow
search: rethrow

  rethrow([e])

  Throw an object without changing the current exception backtrace. The default argument is the current exception (if called within a catch block).

#3

I know about rethrow(), but I want to add additional information to the error message.
For example, if failme() is called from several places, then test() would typically add a note that the problem was caused when calling from test(), possibly plus some other information … if I did not need this, I would not even bother to catch the exception in test() in the first place, since it gets caught in main() anyway.


#4

The stacktrace should contain that information anyway.


#5

Yes, but not any additional information (value of some parameters etc).
Moreover, in my simple example, stacktrace() in the catch-block in main() includes only info about main() itself, while catch_stacktrace(), which I expected to hold the trace of the exception, is empty (StackFrame[]) … weird.

And anyway, this does not explain why the conversion from an Exception to string in error() adds the escaped quotation marks.