Forum: Ruby threads and exceptions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6559f9b9607a89074ea5e238d31ffbc2?d=identicon&s=25 Kelly Felkins (Guest)
on 2006-03-07 03:50
(Received via mailing list)
I've built a small web service using webrick. The service runs on a
separate
thread. When it gets an exception it, apparently, dies...it being the
thread.

Is there a way to catch the exception without the thread exiting? The
code
is opening a file so I wrapped it in a begin/rescue block, but it
doesn't
seem to help.

    begin
      File.open(buffer[:file_name], 'a') do |f|
        f.print(buffer[:text])
      end
    rescue Exception
      @logger.error("could not write message\n  #{$!}\n  file
>#{buffer[:file_name]}<\n  message >#{buffer[:text]}<")
    end

If the file is invalid I think the thread dies -- but I don't see a
message
anywhere.

-Kelly
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-03-07 09:02
(Received via mailing list)
2006/3/7, Kelly Felkins <railsinator@gmail.com>:
>         f.print(buffer[:text])
>       end
>     rescue Exception
>       @logger.error("could not write message\n  #{$!}\n  file
> >#{buffer[:file_name]}<\n  message >#{buffer[:text]}<")
>     end
>
> If the file is invalid I think the thread dies -- but I don't see a message
> anywhere.

For debugging purposes you can use Thread.abort_on_exception=true
which will kill the interpreter with a stack dump. Normally your
rescue clause should do the job - unless there is something outside
the block that throws.

Kind regards

robert
This topic is locked and can not be replied to.