Ruby | Exception Class and its Methods Last Updated : 22 Nov, 2022 Comments Improve Suggest changes 1 Likes 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 Create Quiz Comment A ankita_saini Follow 1 Improve A ankita_saini Follow 1 Improve Article Tags : Ruby Ruby-Built-in-class Ruby-Exception-handling Explore OverviewRuby For Beginners3 min readRuby Programming Language (Introduction)4 min readComparison of Java with Other Programming Languages4 min readSimilarities and Differences between Ruby and C language3 min readSimilarities and Differences between Ruby and C++3 min readEnvironment Setup in Ruby3 min readHow to install Ruby on Linux?2 min readHow to install Ruby on Windows?2 min readInteresting facts about Ruby Programming Language2 min readBasicsRuby | Keywords4 min readRuby | Data Types3 min readRuby Basic Syntax3 min readHello World in Ruby2 min readRuby | Types of Variables4 min readGlobal Variable in Ruby2 min readComments in Ruby2 min readRuby | Ranges4 min readRuby Literals4 min readRuby Directories5 min readRuby | Operators11 min readOperator Precedence in Ruby2 min readOperator Overloading in Ruby5 min readRuby | Pre-define Variables & Constants5 min readRuby | unless Statement and unless Modifier2 min readControl StatementsRuby | Decision Making (if, if-else, if-else-if, ternary) | Set - 13 min readRuby | Loops (for, while, do..while, until)5 min readRuby | Case Statement3 min readRuby | Control Flow Alteration7 min readRuby Break and Next Statement2 min readRuby redo and retry Statement2 min readBEGIN and END Blocks In Ruby2 min readFile Handling in Ruby4 min readMethodsRuby | Methods3 min readMethod Visibility in Ruby3 min readRecursion in Ruby4 min readRuby Hook Methods5 min readRuby | Range Class Methods5 min readThe Initialize Method in Ruby2 min readRuby | Method overriding2 min readRuby Date and Time3 min readOOP ConceptsObject-Oriented Programming in Ruby | Set 19 min readObject Oriented Programming in Ruby | Set-28 min readRuby | Class & Object4 min readPrivate Classes in Ruby3 min readFreezing Objects | Ruby2 min readRuby | Inheritance4 min readPolymorphism in Ruby3 min readRuby | Constructors2 min readRuby | Access Control8 min readRuby | Encapsulation2 min readRuby Mixins3 min readInstance Variables in Ruby3 min readData Abstraction in Ruby3 min readRuby Static Members3 min readExceptionsRuby | Exceptions4 min readRuby | Exception handling6 min readCatch and Throw Exception In Ruby3 min readRaising Exceptions in Ruby4 min readRuby | Exception Handling in Threads | Set - 12 min readRuby | Exception Class and its Methods3 min readRuby RegexRuby | Regular Expressions3 min readRuby Search and Replace2 min readRuby ClassesRuby | Float Class7 min readRuby | Integer Class3 min readRuby | Symbol Class5 min readRuby | Struct Class5 min readRuby | Dir Class and its methods3 min readRuby | MatchData Class4 min readRuby ModuleRuby | Module4 min readRuby | Comparable Module3 min readRuby | Math Module4 min readInclude v/s Extend in Ruby2 min readCollectionsRuby | Arrays4 min readRuby | String Basics5 min readRuby | String Interpolation3 min readRuby | Hashes Basics4 min readRuby | Hash Class12 min readRuby | Blocks6 min read Like