Why Is It Bad to Rescue Exception in Ruby

Oluwafisayo Oluwatayo Feb 15, 2024
  1. The Problem with Rescuing Exceptions in Ruby
  2. Best Approach to Exceptions in Ruby
  3. Situations Where You Can Rescue Exceptions
  4. Conclusion
Why Is It Bad to Rescue Exception in Ruby

Exceptions in Ruby are the errors that programs encounter when we are trying to run them. Some of these exceptions cause our programs to crash, so they are very useful because they tell us what the problems are and where the problem is coming from, then we know what kinds of solutions we can apply.

The Problem with Rescuing Exceptions in Ruby

Exceptions are good because they serve as pointers to the problems we have with our programs. We might become tempted to rescue exceptions within code to catch that particular exception that causes our program to crash, but this can become a problem than a solution.

When programs are being run in Ruby, Ruby itself has exceptions within these programs, so these exceptions do not cause our programs to crash. So rescuing exceptions in Ruby will cause the following issues in our code:

  1. Rescuing exceptions will rescue Interrupt and will prevent the user from exiting the problem with Ctrl+C.
  2. SignalException will also be rescued, which will cause the program to not respond to signals; we will have to force close our code editor.
  3. When SyntaxError is rescued, the evaluations done by the Ruby system will be raised only in the background.
  4. Rescue exceptions raise noMemoryError, an error encountered when Ruby cannot allocate memory. It is an unnecessary error that need not be rescued.
  5. It makes bug hunting difficult since all exceptions are raised, and it becomes hard to pinpoint the actual exception that caused the program to crash.

To see this in action, let us run this code, create a new file, and name it new.rb:

new.rb:

loop do
  sleep 1
  eval 'djsakru3924r9eiuorwju3498 += 5u84fior8u8t4ruyf8ihiure'
rescue Exception
  puts "I can't stop!"
end

rescue exceptions

You will see an infinite loop of I can't stop, then try to stop the program by using Ctrl+C and it just keeps going. The only option is to completely shut down your code editor.

Best Approach to Exceptions in Ruby

Now that we have seen that rescuing exceptions can lead to catastrophic events in our programs, what is the best way to approach exceptions in Ruby?

  1. We can utilize Ruby’s ensure function instead of rescuing exceptions. This makes sure the code runs even if exceptions are encountered.
  2. Rescue StandardError instead, as the exceptions raised here are errors that directly affect the application.

Let us look at the snippet of code below.

new.rb:

loop do
  sleep 1
  eval 'djsakru3924r9eiuorwju3498 += 5u84fior8u8t4ruyf8ihiure'
rescue StandardError => e
  puts "I can't stop!"
end

Once we run this code, we immediately get the syntax error.

rescue standard error

Situations Where You Can Rescue Exceptions

There are occasions where rescuing exceptions can be useful. In situations where we want to log the exceptions, we can re-raise the exception; this will log the exception for subsequent revisits.

If the user knows how to handle the exceptions, it’s safe to rescue them.

Conclusion

Exceptions, no doubt, are pointers to the errors and bugs in our programs; they are useful for knowing what to correct when programs are being updated; it’s just a question of how a user handles them.

Oluwafisayo Oluwatayo avatar Oluwafisayo Oluwatayo avatar

Fisayo is a tech expert and enthusiast who loves to solve problems, seek new challenges and aim to spread the knowledge of what she has learned across the globe.

LinkedIn