Blog |

Runtime Errors in Ruby

Runtime Errors in Ruby
Table of Contents

Exceptions are unintended events that take place when a program is being executed or during its runtime causing disruptions to the program's overall logic. In Ruby, a program is enclosed between the begin and end blocks and a rescue block is used to tell what types of exceptions are to be handled. When no class is specified, by default, a RuntimeError is raised by Kernel#raise.

raise "an exception has occurred"

This is equivalent to:

raise RuntimeError, "an exception has occurred"

Whenever these runtime exceptions occur, for example, a “divide by zero” error or an “index out of bounds” error, they stop the execution of the program completely, so it is good programming practice to handle them properly in our code.

In the exception hierarchy, RuntimeError is a subclass of StandardError, which is a subclass of Exception. All Ruby exceptions are subclasses of the Exception class.

|--> StandardError
      |---> RuntimeError

Example 1: How to Raise an Exception with a Message

In the below code, we raise an exception with a message; by default, Ruby will raise the generic exception RuntimeError.

   raise "Raising our exception"
rescue StandardError => e
  puts "Exception class is #{}"
  puts "Exception message is #{e.message}"
  puts "Exception backtrace is #{ e.backtrace}"

Output of Example 1

Exception class is RuntimeError
Exception message is Raising our exception
Exception backtrace is ["main.rb:2:in `<main>'"]

Example 2: How to Raise a Generic Exception

In the below code, a Runtime Exception (a generic exception) will be raised. The rescue clause is used to instruct Ruby on the type of exception to be handled in our program.

puts "\nRuntime Exception:\n"
    a = 30
    b = 0
    x=(30 + 80) * (a / b) # runtime exception will be thrown 
rescue ZeroDivisionError => e 
  # prints the message=>(divided by 0)
    puts e.message
    puts e.backtrace

Output of Example 2

In the output below, 5 indicates the line number where the error occurs.

Runtime Exception:
divided by 0
main.rb:5:in `/'
main.rb:5:in `<main>'

Example 3: How to Raise a Specific Kind of Exception

In the example below, we’ll modify the second example to raise a specific kind of exception. Though generic exceptions are raised by Ruby by default, it’s good practice to specify the exception class in our raisestatement.

x = 30
y = 0
    # this exception is raised when y is zero
    raise 'y should not be 0' if y == 0
    print "x/y = ", (20 + 30) * (x / y)
rescue ZeroDivisionError => e 
   puts e.message
 # prints the stack trace of the error, but a raised exception has zero stack
   puts e.backtrace

Output of Example 3

In the below output, 5 indicates the line where the problem occurs.

y should not be 0
main.rb:5:in `<main>'

While a RuntimeError is the default exception class raised when an invalid operation is performed and no exception class is specified in the code, specifying the type of exception class in the code is good practice, as without it could be very difficult to debug a very large block of code.

Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Ruby errors easier than ever. Try it today!

Related Posts

See all posts

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape