如何实现异常的默认行为?begin rescue else不起作用(我认为应该).
并且,else在场景中没有意义吗?在没有引发异常时必须运行的任何代码都在begin-rescue块之间运行.
顺便说一下,我有以下解决方法,但我对此并不满意.
class MyException < Exception
end
class YourException < Exception
end
begin
raise MyException if 2 > 50
raise YourException if 1 < 90
rescue Exception => e
case e.message
when /MyException/
puts "MyException Caught"
else
puts "Default Exception Caught"
end
end
Run Code Online (Sandbox Code Playgroud)