Join our Live Demo: "Advanced Error Monitoring & Debugging" on 9/24/2020 12:00PM PDT. Register here
Rescuing pre-defined exceptions is one thing, but to more effectively utilize exceptions within your application, it is important to understand how to create and raise your own. At its core, every Ruby exception stems from a built-in Exception class, and includes a handful of built-in methods, but the most commonly used exception method is message. This method can be used to retrieve a specific exception message from a raised exception object.
Like the title of this section implies, raising exceptions in Ruby can be done using the
raise method. While a blank exception can be raised, however, most exceptions include a message with additional information about the error. For example, a default
RuntimeError exception can be raised with a custom error message in just one line:
raise "This is an exception"
While this is valuable for raising generic exceptions, it is also possible to raise an instance of an
Exception class, such as
raise StandardError.new "This is an exception"
Keep in mind, however, that while
StandardError exceptions are intended to be rescued,
Exception exceptions are not (see Exception Handling in Ruby). This means that if you choose to
raise a new
Exception instance, you will not be able to rescue it without explicitly rescuing every top-level Exception instance, which could cause unexpected problems down the line.
raise Exception.new "This is an exception"
In a nutshell, every custom Ruby exception should extend
StandardError, rather than the
Exception class (the reason for this is outlined in Exception Handling in Ruby). With this in mind, the simplest custom exception class that we can define will look something like this:
class MyCustomException < StandardError end
Given the above class definition, we will be able to raise a
MyCustomException exception, yet have access to all the bells and whistles that come from the built-in
StandardError class. But what if you wanted to define a default error message, or pass additional data along with your custom error? To do this, we simply need to override the constructor to accept custom parameters:
class MyCustomException < StandardError def initialize(msg="This is a custom exception", exception_type="custom") @exception_type = exception_type super(msg) end end
This class definition allows us to raise our custom exception with the additional data:
begin raise MyCustomException.new "Message, message, message", "Yup" rescue MyCustomException => e puts e.message *# Message, message, message* puts e.exception_type *# Yup* end
Outside of raising exceptions, Ruby also supports writing directly to STDERR. Where puts writes messages directly to
STDERR.puts can be used to write raw messages to
STDERR without interrupting program execution. This is useful when you need to track debug data or keep track of caught and handled exceptions.
STDERR.puts "I am an error message"