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
- exception : This method is used to creates and returns a new exception object.
Exception.exception(message)
- new : This method creates and return a new exception object, optionally setting message to message.
Exception.new(message)
# Ruby program to illustrate# use of new method# creating the customized class# inherited from StandardErrorclassMyException < StandardErrorattr_reader:myobjectdefinitialize(myobject)@myobject= myobjectendendbegin# Using new method# to create an object of# the given exceptionraiseMyException.new("My object"),"This is custome class"rescueMyException => eputs e.messageputs e.myobjectendchevron_rightfilter_noneOutput:
This is custome Exception My object
- 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
Example:
# Ruby program to illustrate# use of backtrace method# defining methoddefa1# raise exceptionraise"OOPs! exception raise"end# defining methoddefa2# calling method a1a1()endbegin# calling method a2a2()# rescue exceptionrescue=> a_Details# print the backtrace details# related with exceptionputs a_Details.backtrace.join("\n")endchevron_rightfilter_noneOutput:
(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>
- 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)
- message : This method return a message which is related to exc.
exc.message
- set_backtrace : This method sets the backtrace information related to exc. The arugment of this must be an array of String objects in the format that is described in Exception#backtrace.
exc.set_backtrace(array)
- to_s : This method returns the message related to exc or return name of the exception if no message set.
exc.to_s
Example:
# Ruby program to illustrate# use of to_s methodbegin# raise exceptionraise"Ruby Exception"# rescue exceptionrescueException=> a# print messageputs a.to_sendchevron_rightfilter_noneOutput:
Ruby Exception
- inspect : This method return exception's class name and message.
exc.inspect
- cause : This method return the previous exception at the time when exc raise.
- == : this method return true if the object and exc share same class, message and backtrace. Otherwise, it return false.
exc==obj
- Ruby | Dir Class and its methods
- Ruby | Thread Class-Public Class Methods
- Ruby | Range Class Methods
- Catch and Throw Exception In Ruby
- Ruby | Exception handling
- Ruby | Exception Handling in Threads | Set - 1
- Ruby | Methods
- Ruby Hook Methods
- Ruby - Singleton Methods
- Ruby | Thread Life Cycle & Its States
- Ruby | Class Method and Variables
- Ruby | Class & Object
- Ruby | Float Class
- Ruby | Integer Class
- Ruby | Symbol Class
- Ruby | Struct Class
- Ruby | Hash Class
- Ruby | MatchData Class
- Ruby | Float class +@ value
- Ruby | Float class * value
Instance Methods
Here exc is the object of the Exception class.
Reference: http://ruby-doc.org/core-2.5.1/Exception.html
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

