注册处理程序以处理未处理的异常 [英] Registering handler for unhandled exceptions
本文介绍了注册处理程序以处理未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以为任何未处理的异常定义异常处理程序?将我的整个代码块包装在begin / rescue / end块中感觉很乱。
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.
推荐答案
如何使用at_exit?即使发生异常,也可以调用它,您可以使用$!
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
这篇关于注册处理程序以处理未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文