Register handler for unhandled exceptions

Is it possible to define an exception handler for any unhandled exceptions? Wrapping all the code in the begin / rescue / end block seems messy.

+4
source share
1 answer

How about using at_exit? It should be called even when an exception occurs, and you can write the last exception using $!

Here is an example:

at_exit { puts "Last exception: (#{$!.inspect})" puts "Backtrace: \n#{ $@ }" puts "Exiting..." } puts "my app..." raise "Exception!" 

http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-at_exit

+10
source

Source: https://habr.com/ru/post/1411603/


All Articles