Ruby | Exception Handling in Threads | Set - 1 Last Updated : 12 Sep, 2022 Comments Improve Suggest changes 1 Likes Like Report Threads can also contain exceptions. In Ruby threads, the only exception arose in the main thread is handled but if an exception arises in the thread(other than main thread) cause the termination of the thread. The arising of an exception in a thread other than the main thread depends upon abort_on_exception method. The default value of abort_on_exception is false. When the value of abort_on_exception is false it means the unhandled exception aborts the current thread and the rest of the threads will continue running. You can also change the setting of abort_on_exception by using abort_on_exception=true or $DEBUG to true. Ruby threads also provided a method to handle the exceptions, i.e. ::handle_interrupt. This will method handle exceptions asynchronously. Example: Ruby # Ruby program to illustrate # the exception in thread #!/usr/bin/ruby threads = [] 4.times do |value| threads << Thread.new(value) do |a| # raising an error when a become 2 raise "oops error!" if a == 2 print "#{a}\n" end end threads.each {|b| b.join } Output: 0 3 1 main.rb:12:in `block (2 levels) in ': oops error! (RuntimeError) Note: Thread.Join method is used to wait for the finishing of a particular thread. Because when a Ruby program terminates, all the thread are killed, irrespective of their states. We can also save the exception as shown in the below example: Example: Ruby # Ruby program to illustrate how to # escape the exception #!/usr/bin/ruby threads = [] 5.times do |value| threads << Thread.new(value) do |a| raise "oops error!" if a == 3 print "#{a}\n" end end threads.each do |x| begin x.join # using rescue method rescue RuntimeError => y puts "Failed:: #{y.message}" end end Output: 0 1 4 2 Failed:: oops error! Now set the value of abort_on_exception= true, it kills the thread which contains an exception. Once the thread is dead, no more output will produce. Example: Ruby # Ruby program to illustrate the killing # of thread in which exception raised #!/usr/bin/ruby # setting the value of abort_on_exception Thread.abort_on_exception = true threads = [] 5.times do |value| threads << Thread.new(value) do |a| raise "oops error!" if a == 3 print "#{a}\n" end end # using Thread.Join Method threads.each {|b| b.join } Output: 0 1 2 main.rb:13:in `block (2 levels) in ': oops error! (RuntimeError) The execution of thread(other than main thread) as shown below: Create Quiz Comment A ankita_saini Follow 1 Improve A ankita_saini Follow 1 Improve Article Tags : Misc Ruby Ruby-Multithreading 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