我试图理解Ruby中的异常,但我有点困惑.我使用的教程说,如果发生的异常与rescue语句识别的任何异常都不匹配,可以使用"else"来捕捉它:
begin
# -
rescue OneTypeOfException
# -
rescue AnotherTypeOfException
# -
else
# Other exceptions
ensure
# Always will be executed
end
然而,我在后面的教程中也看到了"rescue"的用法,没有特别说明:
begin
file = open("/unexistant_file")
if file
puts "File opened successfully"
end
rescue
file = STDIN
end
print file, "==", STDIN, "\n"
如果你能做到这一点,那么我还能用别的吗?或者我可以在最后使用通用的救援工具吗?
begin
# -
rescue OneTypeOfException
# -
rescue AnotherTypeOfException
# -
rescue
# Other exceptions
ensure
# Always will be executed
end