Get exception message in catch block

246 views
Skip to first unread message

Avik Sengupta

unread,
Dec 28, 2014, 8:08:50 PM12/28/14
to julia...@googlegroups.com

So if I do sqrt(-1), it throws a DomainError, but I get a fairly long message explaining what is wrong.

julia> sqrt(-1)
ERROR: DomainError
sqrt will only return a complex result if called with a complex argument.
try sqrt(complex(x))
 in sqrt at math.jl:131

If I put the call to sqrt in a try block, how can I access the detailed error message in the catch block? One thing I tried is sprint(showerror, e), but that only returns "DomainError()", without any additional details. .

julia> try
         sqrt(-1)
       catch e
          return sprint(showerror, e)
       end
"DomainError()"

I'm probably missing something obvious...

Tim Holy

unread,
Dec 28, 2014, 8:27:45 PM12/28/14
to julia...@googlegroups.com
Use `rethrow(e)` rather than `showerror`.

But FYI: what's going on behind the scenes is actually quite subtle and
nowhere close to obvious. There's a certain amount of magic that gets
performed to display that long, helpful message: see replutil.jl, and the
`showerror` method for `DomainError`. The long error message is not generated
at the call-site because doing so (IIRC) would have a deleterious effect on the
compiler's ability to inline, and consequently be a big hit on performance.
`rethrow` will include the backtrace from the offending line, allowing the
normal error message to appear.

--Tim

Ismael VC

unread,
Dec 28, 2014, 8:28:10 PM12/28/14
to julia...@googlegroups.com
Avik, this works:

julia> try                                                                                                                   
           sqrt(-1)                                                                                                          
       catch e                                                                                                               
           bt = catch_backtrace()                                                                                            
           showerror(STDERR, e, bt)                                                                                          
       end                                                                                                                   
DomainError
sqrt will only return a complex result if called with a complex argument.
try sqrt(complex(x))
 in sqrt at math.jl:131


Ismael VC

Ismael VC

unread,
Dec 28, 2014, 8:30:46 PM12/28/14
to julia...@googlegroups.com
This is how `showerror(io::IO, e::DomainError, bt)` works: http://bit.ly/1vlfIly



El domingo, 28 de diciembre de 2014 19:08:50 UTC-6, Avik Sengupta escribió:
Message has been deleted

Ismael VC

unread,
Dec 28, 2014, 8:35:47 PM12/28/14
to julia...@googlegroups.com


Use `rethrow(e)` rather than `showerror`.

This is indeed the way to do it, thanks Tim! 

Tim Holy

unread,
Dec 28, 2014, 8:52:39 PM12/28/14
to julia...@googlegroups.com
But your understanding of the subtleties is very good!

--Tim

Avik Sengupta

unread,
Dec 29, 2014, 6:51:45 AM12/29/14
to julia...@googlegroups.com
Thanks Tim and Ishmael. With your help, I got it to do what I needed.

https://github.com/kmsquire/Logging.jl/pull/13

Regards
-
Avik
Reply all
Reply to author
Forward
0 new messages