Currently under development!

Ruby: Rescue Constant ($!)

22 April 2024

1 min

Ruby: Rescue Constant ($!)

Have you ever handled exceptions in your ruby code? If so, then you would have come across rescue. Let’s see an example for a refresher.

def handle_exception
  raise "The exception"
rescue
  p "Exception Rescued"
end


handle_exception      #=> Exception Rescued

If we run the following code, we get Exception Rescued on our terminal. That’s self-explanatory.

But what if we want to use some properties of the exception inside the rescue block? Let’s say we want to print out the exception message rather than Exception Rescued. One way to do that would be as follows:

def handle_exception
  raise "The exception"
rescue => e
  p e.message
end

handle_exception      #=> The exception

This way, we are assigning the exception into the variable e and printing out the message to the console.

As a note, it is advised to specify the exact exception you are catching for better code.

In this case: RuntimeError

Constant Way

Another way of achieving this is to use the ruby $! constant as follows:

def handle_exception
  raise "The exception"
rescue
  p $!.message
end

handle_exception      #=> The exception

You will notice that the e variable is no longer there. we can access the exception from the $! constant. This constant is always nil when used anywhere else in your ruby code. It only always has a value within the rescue block.

This is a nice tip to have while writing or reading code. Thanks for your time and happy coding.