Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Begin Rescue not catching error

Tags:

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?

like image 212
Schneems Avatar asked Jul 02 '10 18:07

Schneems


People also ask

What happens if in a begin rescue block the rescue code has an error?

The code between “begin” and “rescue” is where a probable exception might occur. If an exception occurs, the rescue block will execute. You should try to be specific about what exception you're rescuing because it's considered a bad practice to capture all exceptions.

Can we use rescue without begin?

The method definition itself does the work of begin , so you can omit it. You can also do this with blocks. Now, there is one more way to use the rescue keyword without begin .

How do you rescue all errors in Ruby?

In Ruby, we use the rescue keyword for that. When rescuing an exception in Ruby, you can specify a specific error class that should be rescued from. Note: When using raise without specifying an exception class, Ruby will default to RuntimeError .

How does rescue work in Ruby?

For each rescue clause, the raised Ruby exception is compared against each parameter and the match succeeds if the exception in the clause is the same as or a superclass of the thrown exception. If the thrown Ruby exception does not match any of the specified exception types, the else block gets executed.


2 Answers

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:

Rescuing Interrupt prevents the user from using CTRLC to exit the program.

Rescuing SignalException prevents the program from responding correctly to signals. It will be unkillable except by kill -9.

like image 172
sepp2k Avatar answered Oct 11 '22 23:10

sepp2k


rescue without any parameter accepts exceptions raised by StandardError class. Your error type is SyntaxError which is inherited from a different class called ScriptError. All these error classes are subclasses of Exception class. So as sepp2k suggested use rescue Exception to catch all kinds of exceptions.

like image 31
Suman Mukherjee Avatar answered Oct 11 '22 22:10

Suman Mukherjee