Methods of Exception Class in Ruby

Here, we are going to learn about the various methods of Exception class in Ruby programming with their usages, syntaxes and examples.
Submitted by Hrithik Chandra Prasad, on November 11, 2020

Here, we will see some of the methods of the Exception class. We have already discussed the hierarchy of the Exception class. Exceptions are nothing but the abnormal and unwanted conditions which appear during runtime. The methods of Exception class are used between begin and end block for creating an interface for raise and rescue keywords.
When you create an Exception instance or object, they contain the information of Exceptions like its type, some kind of optional descriptive string, and other kinds of optional information.

Some of the major methods of Exception class are listed below along with their demonstrating program codes and syntaxes.

1) Exception class methods

They are exception and new.

i) new

As the name suggests, this method is used to create a new instance or object of Exception class with setting the message string optionally.

Syntax:

Exception.new(message)

Example:

=begin
    Ruby program to demonstrate use of new method 
=end

class NewException < StandardError 
attr_reader :myobj

def initialize(myobj) 
  @myobj = myobj 
end
end

begin

raise NewException.new("This is my object created with new"), "This is self made class"
rescue NewException => e 
puts e.message 
puts e.myobj 
end

Output:

This is self made class
This is my object created with new

Explanation:

In the above code, you can see that we have created a custom or self-made exception named NewException with the help of extending the Standard error class then we have created a constructor of it with the help of the initialize method. Coming towards the begin end block where we have actually implemented a new method for creating the object of NewException class. You can see that the string between round brackets are sent to the constructor and the rest is sent to the message method.

ii) exception

Alike new method, this method is also used to create a new instance or object of Exception class with setting the message string optionally.

Syntax:

Exception.exception(message)

Example:

=begin
    Ruby program to demonstrate use of exception method 
=end

class NewException < StandardError 
attr_reader :myobj

def initialize(myobj) 
  @myobj = myobj 
end
end

begin

raise NewException.exception("This is my object created with exception"), "This is self made class"
rescue NewException => e 
puts e.message 
puts e.myobj 
end

Output:

This is self made class
This is my object created with exception

Explanation:

In the above code, you can see that we have created a custom or self-made exception named as NewException with the help of extending the Standard error class then we have created a constructor of it with the help of initialize method. Coming towards the begin end block where we have actually implemented exception method for creating the object of NewException class. You can see that the string between round brackets are sent to constructor and rest is sent to the message method.

2) Public instance methods

i) backtrace

This method is used to show any backtrace, if there exists any in the object of Exception classes. This is a kind of String array that contains either method named as filename:line:in or filename:line. backtrace tells us in which line the error or exception occurred.

Syntax:

exception_object.backtrace

Example:

=begin
    Ruby program to demonstrate use of backtrace method
=end 
 
def meth1 
    raise "exception raised! Rescue"
end

def meth2  
    meth1() 
end

begin 
    meth2()  
    rescue => meth_Details 
    puts meth_Details.backtrace.join("\n") 
end

Output:

main.rb:6:in `meth1'
main.rb:10:in `meth2'
main.rb:14:in `<main>'

Explanation:

In the above code you can observe that our program is displaying information about the backtrace which occurred in the object with the help of backtrace method.

ii) message

This method is a public instance method which is used to print the message which might be stored while declaring the object.

Syntax:

Exception_object.message

Example:

=begin
    Ruby program to demonstrate use of new method 
=end

class NewException < StandardError 
    attr_reader :myobj
    
    def initialize(myobj) 
      @myobj = myobj 
    end
end

begin
    raise NewException.new("This is my object created with new"), "This is self made class"
    rescue NewException => e 
    puts e.message 
    puts e.myobj 
end

Output:

This is self made class
This is my object created with new

Explanation:

In the above code you can observe that with the help of message method, we are able to see the message which is associated with the object of Exception class passed at the time of creation of object.

iii) set_backtrace

This method is a public instance method of Exception class which is used to set information related to backtrace in the object of the Exception class.

Example:

=begin
    Ruby program to demonstrate use of set_backtrace method
=end 
class ErrorCreate
  def self.new(error, message = nil, backtrace: caller)
    exception = error.new(message)
    exception.set_backtrace(backtrace)
    exception
  end
end
ErrorCreate.new(StandardError,"Hello from includehelp.com", backtrace: caller)

Output:

#<StandardError: Hello from includehelp.com>

Explanation:

In the above code you can observe that we have created our own error and implemented set_backtrace in it. With the help of this method we are setting information about the error.

iv) to_s

This message is a public instance method that is used to return the message passed along with the object of the Exception class when the Exception object was created. It returns the name of the object if no message is passed.

Syntax:

Exception_object.to_s

Example:

=begin
    Ruby program to demonstrate use of to_s method 
=end

begin 
    raise "Exception Raised. From includehelp.com"
    rescue Exception => e 
    puts e.to_s 
end

Output:

Exception Raised. From includehelp.com

v) inspect

This is a public instance method which is used to return the name of exception class along with the message.

Example:

=begin
    Ruby program to demonstrate use of inspect method 
=end

class NewException < StandardError 
    attr_reader :myobj
    
    def initialize(myobj) 
      @myobj = myobj 
    end
end

begin
    raise NewException.new("This is my object created with new"), "This is self made class"
    rescue NewException => e 
    puts e.message 
    puts e.myobj
    puts e.inspect 
end

Output:

This is self made class
This is my object created with new
#<NewException: This is self made class>

Explanation:

In the above code, you can observe that we have created an object of NewException class and the method inspect is informing us about the name of class and the message associated with the object.

vi) == operator

This is a public instance method which is used to compare the classes of Exceptions. If two exceptions are of same class, this method returns true otherwise false.

Syntax:

Object_of_Exception == Object2_of_Exception

Example:

=begin
    Ruby program to demonstrate use of == method 
=end

class NewException < StandardError 
    attr_reader :myobj
    
    def initialize(myobj) 
      @myobj = myobj 
    end
end

begin
    raise NewException.new("This is my object created with new"), "This is self made class"
    rescue NewException => e 
    obj = NewException.new("Hello")
    obj1 = NewException.new("Bye")
    puts e.message 
    puts e.myobj
    puts obj1 == obj
end

Output:

This is self made class
This is my object created with new
true

Explanation:

In the above output you can observe that we have created two objects of NewException class and after comparing both of them with ==, we are getting true as the result.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.