class MyKlass
include ActiveSupport::Rescuable
# define a method, which will do something for you, when exception is caught
rescue_from Exception, with: :my_rescue
def some_method(&block)
yield
rescue Exception => exception
rescue_with_handler(exception) || raise
end
# do whatever you want with exception, for example, write it to logs
def my_rescue(exception)
puts "Exception catched! #{exception.class}: #{exception.message}"
end
end
MyKlass.new.some_method { 0 / 0 }
# Exception catched! ZeroDivisionError: divided by 0
#=> true
1条答案
按热度按时间ztigrdn81#
以下是它应该如何使用:
字符串
不用说,营救
Exception
是一种犯罪。