ruby - Difference between $! versus a variable with rescue -
when rescuing exception, there 2 ways refer raised exception:
begin ... rescue exception => e handle_the_error(e) end and
begin ... rescue exception handle_the_error($!) end i believe interchangeable, they? there situation 1 should used on other?
i think these snippets interchangeable. should prefer explicit variables thread-global magic.
one case $! magic var handy:
result_or_error = perform_some_operation() rescue $! for don't know line means:
it's called "inline rescue". format this:
<expr1> rescue <expr2> first, expr1 evaluated. if no exception raised, value returned. if there exception, expr2 evaluated , value returned.
so, in case, if perform_some_operation() raised exception, variable result_or_error set instance of exception (because $! returns last error).
Comments
Post a Comment