# mangled error message when re-throwing exceptions

**URL:** <https://discourse.julialang.org/t/mangled-error-message-when-re-throwing-exceptions/9253>\
**Category:** General Usage\
**Tags:** exception\
**Created:** [February 22, 2018, 2:39pm UTC](https://discourse.julialang.org/t/mangled-error-message-when-re-throwing-exceptions/9253 "2018-02-22T14:39:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkaut](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@mkaut](https://discourse.julialang.org/u/mkaut)\
**Post date:** [February 22, 2018, 2:39pm UTC](https://discourse.julialang.org/t/mangled-error-message-when-re-throwing-exceptions/9253/1 "2018-02-22T14:39:14Z")

</div>

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](https://github.com/chrisvoncsefalvay/learn-julia-the-hard-way/blob/master/_chapters/11-ex8.md#creating-your-own-exceptions)), 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…

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [February 22, 2018, 3:42pm UTC](https://discourse.julialang.org/t/mangled-error-message-when-re-throwing-exceptions/9253/2 "2018-02-22T15:42:43Z")

</div>

Perhaps you are looking for `rethrow`?

```julia
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).

```

---

<div class="post-metadata">

**Author:** ![mkaut](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@mkaut](https://discourse.julialang.org/u/mkaut)\
**Post date:** [February 23, 2018, 7:31am UTC](https://discourse.julialang.org/t/mangled-error-message-when-re-throwing-exceptions/9253/3 "2018-02-23T07:31:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 23, 2018, 7:36am UTC](https://discourse.julialang.org/t/mangled-error-message-when-re-throwing-exceptions/9253/4 "2018-02-23T07:36:26Z")

</div>

> [@mkaut](#):
>
> add a note that the problem was caused when calling from

The stacktrace should contain that information anyway.

---

<div class="post-metadata">

**Author:** ![mkaut](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@mkaut](https://discourse.julialang.org/u/mkaut)\
**Post date:** [February 23, 2018, 8:05am UTC](https://discourse.julialang.org/t/mangled-error-message-when-re-throwing-exceptions/9253/5 "2018-02-23T08:05:28Z")

</div>

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.
