Threads and exceptions


#1

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


#2

2006/3/7, Kelly F. removed_email_address@domain.invalid:

    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