Registering handler for unhandled exceptions

瘦欲@ 提交于 2019-12-23 12:40:31

问题


Is it possible to define an exception handler for any unhandled exceptions? Wrapping my entire code block in a begin/rescue/end block feels messy.


回答1:


How about using at_exit? It should be called even when an exception occurs and you can log 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



来源:https://stackoverflow.com/questions/10521559/registering-handler-for-unhandled-exceptions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!