Exception Handling in Ruby

Exception handling is a way to handle various types of errors, which may occur during program execution. If you write code, you must-have encountered errors also called Exceptions. You can take appropriate actions with proper exception handling instead of halting the program completely.

Why Handling Exceptions is Necessary

  • The exceptions should be handled to prevent any abnormal termination of a program.
  • Crashing programs or failing API requests can be very frustrating for customers.  If it happens repeatedly, you could lose your customers.

Ruby, fortunately, provides a nice mechanism to handle exceptions. Ruby provides a separate class for handling exceptions called Exception class which contains different types of methods.

Handling Exceptions in Ruby

The code in which an exception is raised is enclosed between the begin/end block. You can use a rescue clause to handle this type of exception. Find below an example syntax of handling exceptions.

# code execution starts
begin

     # block where code is written.
     # exception can be raised here

    rescue
     # block where exception is rescued
      
end
# code execution ends

Using retry Statement

You capture an exception using a rescue block. You can then use the retry statement to execute begin block from the beginning.

# code execution starts
begin

     # block where code is written.
     # Exception can be raised here
  
    rescue
    
     # block where exception is rescued
      
    retry
    
    # this statement restarts the rescue
    # block from the beginning	
    
end
# code execution ends

In the above example, below is the flow of execution:

  • Code execution starts
  • Exception is raised
  • Code execution flow goes to the rescue block. It finds a retry statement.
  • Because of the retry statement, code execution again starts from the beginning.

You should be very careful with retry statements. If the exception is continuously triggered, retry will keep happening. This can trigger an infinite loop.

Using raise Statement

You can use a raise statement to raise an exception. The following method raises an exception whenever it's called. The first argument of the raise statement is used to create an exception and then set the message in the second argument.

# code execution starts
begin
 
      # block where code is written
      
      raise "raising exception"
      
      # code block after raising exception
      
    rescue
      # block where exception is rescued
      
end 
# code execution end

In the above example, below is the flow of execution:

  • Code execution starts.
  • The exception is raised because of the raise keyword.
  • The code block after raising the exception is skipped.
  • Code execution flow goes to the rescue block. rescue block is executed.
  • Code executions end.

Using ensure Statement

Sometimes, you want to make sure that a specific piece of code will always run! That’s what the ensure keyword is used for. This keyword executes a section of code that is always run when an exception is raised.

# code execution starts
begin
 
      # block where code is written
      
      raise "raising exception"
      
      # code block after raising exception
      
    rescue
      # block where exception is rescued
     
    ensure
      # Always executed when exception is raised	
      
end 
# code execution end

In the above example, below is the flow of execution:

  • Code execution starts.
  • The exception is raised because of the raise keyword.
  • The code block after raising the exception is skipped.
  • Code execution flow goes to the rescue block. rescue block is executed.
  • After executing the rescue block, the code block under ensure the section is executed.
  • Code executions end.

Conclusion

The code execution and the exception always go together. If code is written without proper exception handling, then your program is considered to be of bad quality. As you can find above, Ruby has a lot of different keywords for handling exceptions, not just begin and rescue. Play around with these in your methods to see how you can rescue your code.

Abhay

Abhay

Pune, India