Ruby | Exception Handling in Threads | Set - 1 Last Updated : 12 Sep, 2022 Suggest changes Share Like Article 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: A ankita_saini Follow Article Tags : Misc Ruby Ruby-Multithreading Ruby-Exception-handling Explore Ruby Programming Language 4 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 My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like