EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Ruby

Exceptions

Computer Science / Ruby tutorial chapter - Published 2025-12-16 - Ruby

In programming, predicting errors and handling them effectively is crucial. Exceptions are errors that occur during runtime and disrupt the normal flow of a program. Handling exceptions properly ensures a program continues to operate even in unexpected situations.

What is an Exception?

An exception is an unexpected event that occurs during the execution of a program, disrupting its normal flow. It provides a way to handle error scenarios without stopping the entire program.

Errors vs. Exceptions

Errors:

  • Unexpected issues that arise during program execution.
  • Cannot be handled directly.
  • All errors are considered exceptions.

Exceptions:

  • Unexpected events during runtime.
  • Can be managed using begin-rescue blocks.
  • Not all exceptions are errors.

Traditional Exception Handling Approach

In the traditional approach, errors were managed using return codes. Methods would return specific values indicating failure, which would be propagated through calling routines until a function took responsibility. This made error management complex and hard to maintain.

Ruby addresses this issue by using an exception class that packages information about errors into an object. This object is passed through the calling stack until appropriate code is found to handle the error.

Exception Class & Its Hierarchy

Ruby has a built-in hierarchy of exception classes. Most exceptions are subclasses of StandardError, which represents general errors in Ruby programs. More serious exceptions fall under other classes. Users can also create their own exceptions by subclassing StandardError or its descendants. Every exception object contains a message and a stack trace.

Example of an Exception

# Ruby program to illustrate an exception

# defining two integer values
num1 = 14
num2 = 0

# attempting to divide by zero
result = num1 / num2

puts "The result is: #{result}"

Runtime Error:

source_file.rb:6:in `/': divided by 0 (ZeroDivisionError)
from source_file.rb:6:in `<main>'

Explanation

In the example above, dividing 14 by 0 triggers a ZeroDivisionError.

Creating User-Defined Exceptions

Ruby uses the raise method to create exceptions, which become instances of the Exception class or its subclasses. The rescue clause is used to handle these exceptions.

Example of User-Defined Exception

# Ruby program to create a user-defined exception

# defining a method
def raise_exception
  puts 'This is before the exception is raised!'

  # using raise to create an exception
  raise 'Custom Exception Raised'

  puts 'This line will not be executed.'
end

# calling the method
raise_exception

Output:

This is before the exception is raised!
source_file.rb:6:in `raise_exception': Custom Exception Raised (RuntimeError)
    from source_file.rb:10:in `<main>'

Handling Exceptions with rescue

You can handle exceptions in Ruby using begin-rescue blocks. This structure allows the program to continue running after handling an exception.

Example of Handling Exception

# Ruby program to create and handle a user-defined exception

# defining a method
def raise_and_rescue
  begin
    puts 'This is before the exception is raised!'

    # using raise to create an exception
    raise 'Custom Exception Raised!'

    puts 'This line will not be executed.'

  # using rescue to handle the exception
  rescue
    puts 'Exception handled!'
  end

  puts 'Outside of the begin block!'
end

# calling the method
raise_and_rescue

Output:

This is before the exception is raised!
Exception handled!
Outside of the begin block!
End of lesson.