我正在使用一些包含在 begin -rescue 块中的 ruby 代码,但不知何故它仍然崩溃。
代码块如下所示:
# Retrieve messages from server
def get_messages
@connection.select('INBOX')
@connection.uid_search(['ALL']).each do |uid|
msg = @connection.uid_fetch(uid,'RFC822').first.attr['RFC822']
begin
process_message(msg)
add_to_processed_folder(uid) if @processed_folder
rescue
handle_bogus_message(msg)
end
# Mark message as deleted
@connection.uid_store(uid, "+FLAGS", [:Seen, :Deleted])
end
end
鉴于这段代码我会假设如果处理消息 or 添加到已处理文件夹无法执行然后救援将启动并呼叫处理假消息。话虽这么说,我在生产环境中运行此代码,有时当我“收到”一封电子邮件(这是从 rake 任务运行)时,它会因以下错误而终止:语法错误.
要查看错误消息,请查看http://pastie.org/1028479 http://pastie.org/1028479不是那个处理消息它所指的是相同的处理消息多于。有什么理由吗begin - rescue不会捕获这个异常吗?
rescue
不带参数只会拯救继承自的异常StandardError
。为了营救一个SyntaxError
use rescue SyntaxError
.
要拯救所有异常,您可以使用rescue Exception
,但请注意,这是一个坏主意(这就是为什么它不是默认行为rescue
)如所解释的here http://www.mikeperham.com/2012/03/03/the-perils-of-rescue-exception/ and here https://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby。尤其是这一部分:
拯救中断可防止用户使用 CTRLC 退出程序。
拯救 SignalException 会阻止程序正确响应信号。除了用kill -9 之外,它是无法杀死的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)