Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between $! versus a variable with rescue

Tags:

exception

ruby

When rescuing from an exception, there are two ways to refer to the raised exception:

begin
  ...
rescue Exception => e
  handle_the_error(e)
end

and

begin
  ...
rescue Exception
  handle_the_error($!)
end

I believe they are interchangeable, but are they? Is there any situation where one should be used over the other?

like image 256
sawa Avatar asked May 19 '13 17:05

sawa


People also ask

What does rescue do Ruby?

A raised exception can be rescued to prevent it from crashing your application once it reaches the top of the call stack. 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.

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 .

What is begin and rescue in rails?

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.


1 Answers

I also think these snippets are interchangeable. But you should always prefer explicit variables to thread-global magic.

One case where $! magic var is handy:

result_or_error = perform_some_operation() rescue $!

For those who don't know that this line means:

It's so called "inline rescue". Format is this:

<expr1> rescue <expr2>

First, expr1 is evaluated. If no exception was raised, its value is returned. But if there was an exception, then expr2 is evaluated and its value returned.

So, in this case, if perform_some_operation() raised an exception, variable result_or_error would be set to an instance of that exception (because $! returns last error).

like image 194
Sergio Tulentsev Avatar answered Oct 30 '22 21:10

Sergio Tulentsev