Open In App

Ruby | Exception Class and its Methods

Last Updated : 22 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

An exception is an unwanted or unexpected event, which occurs during the execution of a program, i.e. at runtime, that disrupts the normal flow of the program’s instructions. In Ruby, descendants of an Exception class are used to interface between raise methods and rescue statements in the begin or end blocks. 
Exception objects carry information about the exception like its type, an optional descriptive string, and optional information. 
Inbuilt subclasses of Ruby Exception are:
 

 

Exception Class Methods

 

  1. exception : This method is used to creates and returns a new exception object. 
     
Exception.exception(message)
  1. new : This method creates and return a new exception object, optionally setting message to message. 
     
Exception.new(message)
  1.  

Ruby




# 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 custom class"
rescue MyException => e
  puts e.message
  puts e.myobject
end


  1. Output: 
     
This is custom Exception
My object
    1. backtrace : This method returns any backtrace related to exc. The backtrace is the array of string which contains either filename:line:in method or filename:line
       
exc.backtrace
  1. Example:
     

Ruby




# Ruby program to illustrate
# use of backtrace method
 
# defining method
def a1
 
# raise exception
raise "OOPs! exception raise"
end
 
# defining method
def a2
# calling method a1
a1()
end
 
begin
# calling method a2
a2()
# rescue exception
rescue => a_Details
 
# print the backtrace details
# related with exception
puts a_Details.backtrace.join("\n")
end


  1. Output: 
     
  
(repl):8:in `a1'
(repl):14:in `a2'
(repl):19:in `<main>'
/run_dir/repl.rb:38:in `eval'
/run_dir/repl.rb:38:in `run'
/run_dir/repl.rb:54:in `handle_eval'
/run_dir/repl.rb:170:in `start'
/run_dir/repl.rb:177:in `start'
/run_dir/repl.rb:181:in `<main>'
/pre>
  1. exception : With no argument, or if the argument is the same as the receiver, return the receiver. Otherwise, create a new exception object of the same class as the receiver, but with a message equal to string.to_str
exc.exception(message)
  1. message : This method return a message which is related to exc.
     
exc.message
  1. set_backtrace : This method sets the backtrace information related to exc. The argument of this must be an array of String objects in the format that is described in Exception#backtrace.
     
exc.set_backtrace(array)
  1. to_s : This method returns the message related to exc or return name of the exception if no message set. 
     
exc.to_s
  1. Example:
     

Ruby




# 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


  1. Output: 
     
Ruby Exception
  1. inspect : This method return exception’s class name and message. 
     
exc.inspect
  1. cause : This method return the previous exception at the time when exc raise.
  2. == : this method return true if the object and exc share same class, message and backtrace. Otherwise, it return false.
     
exc==obj


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads