Ruby else 块中的异常处理
Ruby exception handling in else block
所以我有经典的 ruby 异常处理:
begin
# do work here
rescue SafeShutdown => e
# prevent loss of data and safely shutdown
rescue SystemExit => e
# print #{e} and continue
else
# how can I get #{e} here to get error message
# so I can behave like in previous rescue
#
# print #{e} and continue
end
我的问题是如何让 "e" 在块的其他部分的记录器中打印出来。
在 begin rescue
块中 else
仅在没有异常发生时调用,即没有抛出错误。试试这个:
begin
# do work here
rescue SafeShutdown => e
# print e
rescue SystemExit => e
# print e
else
# this will only run when no exceptions are thrown
ensure
# this will always run
end
所以我有经典的 ruby 异常处理:
begin
# do work here
rescue SafeShutdown => e
# prevent loss of data and safely shutdown
rescue SystemExit => e
# print #{e} and continue
else
# how can I get #{e} here to get error message
# so I can behave like in previous rescue
#
# print #{e} and continue
end
我的问题是如何让 "e" 在块的其他部分的记录器中打印出来。
在 begin rescue
块中 else
仅在没有异常发生时调用,即没有抛出错误。试试这个:
begin
# do work here
rescue SafeShutdown => e
# print e
rescue SystemExit => e
# print e
else
# this will only run when no exceptions are thrown
ensure
# this will always run
end