开始救援不捕捉错误

开始救援不捕捉错误

本文介绍了开始救援不捕捉错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些包含在开始 - 救援模块中的ruby代码,但不知何故,它仍然崩溃。



代码块如下所示:

 #检索邮件从服务器
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
#将消息标记为已删除
@ connection.uid_store(uid,+ FLAGS,[:Seen,... Deleted] )
end
end

给定这个代码我会假设如果 process_message add_to_processed_folder 无法执行,那么救援将会启动并调用 handle_bogus_message 。这就是说,我在生产环境中运行这个代码,有时当我收到一封电子邮件(这是从耙子任务运行的时候),它死了一个 SyntaxError



有关错误信息,请查看,而不是那个 process_message 是与上述相同的 process_message 。是否有任何原因开始 - 救援不会捕获此异常?

解决方案

rescue 没有参数只是拯救继承自 StandardError的。要拯救一个 SyntaxError 使用 rescue SyntaxError



拯救您将使用的所有例外 rescue Exception ,但请注意,这是一个坏主意(这就是为什么它不是默认行为 rescue ),如所述和。特别是这部分:


I'm using some ruby code wrapped in a begin - rescue block but somehow it manages to still crash.

the block of code looks like this:

# 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

Given this code i would assume that if process_message or add_to_processed_folder could not execute then rescue would kick in and call handle_bogus_message. That being said I'm running this code in a production environment and sometimes when i "get" an email message (this is run from a rake task) it dies with a SyntaxError.

For a look at the error message check out http://pastie.org/1028479 and not that process_message that it is referring to is the same process_message above. Is there any reason why begin - rescue won't catch this exception?

解决方案

rescue without a parameter just rescues exceptions that inherit from StandardError. To rescue a SyntaxError use rescue SyntaxError.

To rescue all exceptions you would use rescue Exception, but note that that's a bad idea (which is why it's not the default behavior of rescue) as explained here and here. Especially this part:

这篇关于开始救援不捕捉错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:06