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?
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.
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 .
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With