Ruby | Exception Class and its Methods Last Updated : 22 Nov, 2022 Comments Improve Suggest changes Like Article Like 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 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 # 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 Output: This is custom Exception My objectbacktrace : 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.backtraceExample: 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 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>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.messageset_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)to_s : This method returns the message related to exc or return name of the exception if no message set. exc.to_sExample: 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 Output: Ruby Exceptioninspect : This method return exception's class name and message. exc.inspectcause : 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 Comment More infoAdvertise with us A ankita_saini Follow Improve Article Tags : Ruby Ruby-Built-in-class Ruby-Exception-handling Explore Ruby Programming Language 2 min read OverviewRuby For Beginners 3 min read Ruby Programming Language (Introduction) 4 min read Comparison of Java with Other Programming Languages 4 min read Similarities and Differences between Ruby and C language 3 min read Similarities and Differences between Ruby and C++ 3 min read Environment Setup in Ruby 3 min read How to install Ruby on Linux? 2 min read How to install Ruby on Windows? 2 min read Interesting facts about Ruby Programming Language 2 min read BasicsRuby | Keywords 4 min read Ruby | Data Types 3 min read Ruby Basic Syntax 3 min read Hello World in Ruby 2 min read Ruby | Types of Variables 4 min read Global Variable in Ruby 2 min read Comments in Ruby 2 min read Ruby | Ranges 4 min read Ruby Literals 4 min read Ruby Directories 5 min read Ruby | Operators 11 min read Operator Precedence in Ruby 2 min read Operator Overloading in Ruby 5 min read Ruby | Pre-define Variables & Constants 5 min read Ruby | unless Statement and unless Modifier 2 min read Control StatementsRuby | Decision Making (if, if-else, if-else-if, ternary) | Set - 1 3 min read Ruby | Loops (for, while, do..while, until) 5 min read Ruby | Case Statement 3 min read Ruby | Control Flow Alteration 7 min read Ruby Break and Next Statement 2 min read Ruby redo and retry Statement 2 min read BEGIN and END Blocks In Ruby 2 min read File Handling in Ruby 4 min read MethodsRuby | Methods 3 min read Method Visibility in Ruby 3 min read Recursion in Ruby 4 min read Ruby Hook Methods 5 min read Ruby | Range Class Methods 5 min read The Initialize Method in Ruby 2 min read Ruby | Method overriding 2 min read Ruby Date and Time 3 min read OOP ConceptsObject-Oriented Programming in Ruby | Set 1 9 min read Object Oriented Programming in Ruby | Set-2 8 min read Ruby | Class & Object 4 min read Private Classes in Ruby 3 min read Freezing Objects | Ruby 2 min read Ruby | Inheritance 4 min read Polymorphism in Ruby 3 min read Ruby | Constructors 2 min read Ruby | Access Control 8 min read Ruby | Encapsulation 2 min read Ruby Mixins 3 min read Instance Variables in Ruby 3 min read Data Abstraction in Ruby 3 min read Ruby Static Members 3 min read ExceptionsRuby | Exceptions 4 min read Ruby | Exception handling 6 min read Catch and Throw Exception In Ruby 3 min read Raising Exceptions in Ruby 4 min read Ruby | Exception Handling in Threads | Set - 1 2 min read Ruby | Exception Class and its Methods 3 min read Ruby RegexRuby | Regular Expressions 3 min read Ruby Search and Replace 2 min read Ruby ClassesRuby | Float Class 7 min read Ruby | Integer Class 3 min read Ruby | Symbol Class 5 min read Ruby | Struct Class 5 min read Ruby | Dir Class and its methods 3 min read Ruby | MatchData Class 4 min read Ruby ModuleRuby | Module 4 min read Ruby | Comparable Module 3 min read Ruby | Math Module 4 min read Include v/s Extend in Ruby 2 min read Like