Do you think IIT Guwahati certified course can help you in your career?
No
An exception is something that symbolizes a unique circumstance; it shows that something is wrong. Ruby programs automatically get terminated when an exception occurs, which becomes problematic for the developers as any crucial code, after an exception is raised, is not executed.
We declare exception handlers to handle such exceptions. In the following article, we will discuss handling exceptions using the exception classes and exception objects in detail.
Exceptions and Exception Handling
Some common Programming errors that come under exceptions include attempting to divide by zero, calling a method on an object that doesn't define it, and calling a method with an invalid parameter. Exceptions can also be a result of an external circumstance, such as attempting to create an object when memory is low or sending a network request when it is unavailable.
Ruby raises exceptions using the Kernel method raise and handles them using the rescue clause. The Exception class or one of its several subclasses is represented by exceptions raised using raise. A symbol thrown by throw propagates in the same way as an exception raised by raise, even though the throw and catch methods are not designed to signal and handle exceptions.
Exception Classes and Exception Objects
Instances of the Exception class or one of its subclasses make up exception objects. There are numerous subcategories. Although these subclasses usually do not specify new behaviors or methods, they allow for the type-based classification of exceptions.
Exception Class Methods
exception
This method is used to create and return a new exception object. Syntax-
Exception.exception(message)
You can also try this code with Online Ruby Compiler
# Ruby program to illustrate
# use of new method
# creating the customized class
# inherited from StandardError
class MyException < StandardError
attr_reader :myobject
def initialize(myobject)
@myobject = myobject
end
end
begin
# Using new method
# to create an object of
# the given exception
raise MyException.new("My object"), "This is custome class"
rescue MyException => e
puts e.message
puts e.myobject
end
You can also try this code with Online Ruby Compiler
This method returns any backtrace related to exc. The backtrace is the array of strings that contains either filename:line:in method or filename:line. Syntax-
exc.backtrace
You can also try this code with Online Ruby Compiler
Return the receiver if there is no argument or if the argument is the same as the receiver. If not, create a brand-new exception object with the receiver's class and a message that is equal to string.to str.
Syntax-
exc.exception(message)
You can also try this code with Online Ruby Compiler
This method sets the backtrace information related to exc. This argument must be an array of String objects in the format described in Exception#backtrace. Syntax-
exc.set_backtrace(array)
You can also try this code with Online Ruby Compiler
# Ruby program to illustrate
# use of to_s method
begin
# raise exception
raise "Ruby Exception"
# rescue exception
rescue Exception => a
# print message
puts a.to_s
end
You can also try this code with Online Ruby Compiler
This method returns the previous exception at the time when exc raise.
==
This method returns true if the object and exc share the same class, message, and backtrace. Otherwise, it returns false.
Frequently Asked Questions
How do you handle exceptions in Ruby?
An additional exception class known as the Exception class, which contains several types of methods, is also provided by Ruby. Since the code that causes this kind of an exception to be raised is located within the begin/end block, you can manage it using a rescue clause.
How do you add exceptions to Ruby?
Classes, like everything else in Ruby, are exceptions! Construct a class that inherits from StandardError or one of its offspring to generate a new type of exception. New exceptions typically have class names that conclude in "Error."
What is rescue in Ruby?
When an exception reaches the top of the call stack, it can be saved to keep your program from crashing. The rescue keyword in Ruby is used for this.
What is a raise in Ruby?
In Ruby, the keyword raise allows us to raise any encountered exception. If this happens, the raise will throw an exception, which will then be caught in the rescue statement.
What is an enumerator in Ruby?
Ruby has a class called enumerator that supports both internal and external iterations. External iteration denotes that the environment or the client dictates how the iteration is carried out. In contrast, internal iteration in the form of iteration controlled by the class in question.
Conclusion
In this article, we have extensively discussed the various ways we can handle exceptions in Ruby. We began with a brief introduction about exceptions, followed by its class, methods, and examples.
After reading about Exception Classes and Exception Objects in Ruby, refer to Ruby Documentation, Ruby-Coding Ninjas, and Learn Ruby for a deeper understanding of Ruby development and other related topics.