threads and exceptions

K

Kelly Felkins

------=_Part_4345_30580935.1141699807509
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I've built a small web service using webrick. The service runs on a separat=
e
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

------=_Part_4345_30580935.1141699807509--
 
R

Robert Klemme

2006/3/7 said:
I've built a small web service using webrick. The service runs on a separ= ate
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 cod= e
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 messa= ge
anywhere.

For debugging purposes you can use Thread.abort_on_exception=3Dtrue
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top