#!/usr/bin/env ruby
#==========================================================================
# Author: martin.vahi@softf1.com
# This file is in public domain.
#==========================================================================
require 'thread'

def func_flaw_demo
   #----------------------------------------------------------------------
   begin
      raise(Exception.new("\n"+
      "The begin-rescue works in the main thread."))
   rescue Exception=>e
      puts e.to_s
   end # rescue
   #----------------------------------------------------------------------
   ob_thread_1=Thread.new do
      begin
         raise(Exception.new(
         "The begin-rescue works in a newly created thread, the ob_thread_1."+
         "\n\n"))
      rescue Exception=>e
         puts e.to_s
      end # rescue
   end # ob_thread_1
   # The
   ob_thread_1.join
   # makes the main thread to wait for ob_thread_1 to finish before exiting.
   #----------------------------------------------------------------------
   ob_thread_2=nil # declares a var outside begin-rescue
   begin
      ob_thread_2=Thread.new do
         raise(Exception.new("\n\n"+
         "The begin-rescure construct in the main thread \n"+
         "will not catch the exception that is thrown from \n"+
         "a newly created thread, the ob_threaqd_2, despite the fact that the \n"+
         "ob_thread_2 started to execute within the begin-rescue block \n"+
         "of the main thread.\n"+
         "I think that the correct solution would be to treat threads in\n"+
         "that situation like lambda functions are treated."+
         "\n\n"))
      end # ob_thread_2
   rescue Exception=>e
      puts e.to_s
   end # rescue
   ob_thread_2.join
end # func_flaw_demo

func_flaw_demo()

