# Threads and exceptions

**URL:** <https://rubytalk.org/t/threads-and-exceptions/25498>\
**Category:** ruby-talk\
**Created:** [7 March 2006 02:50 UTC](https://rubytalk.org/t/threads-and-exceptions/25498 "2006-03-07T02:50:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kelly\_Dwight\_Felkins](https://avatars.discourse-cdn.com/v4/letter/k/ecccb3/32.png) [@Kelly\_Dwight\_Felkins](https://rubytalk.org/u/Kelly_Dwight_Felkins)\
**Post date:** [7 March 2006 02:50 UTC](https://rubytalk.org/t/threads-and-exceptions/25498/1 "2006-03-07T02:50:22Z")

</div>

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.

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

> #{buffer[:file\_name]}\<\n message \>#{buffer[:text]}\<")

&nbsp;&nbsp;&nbsp;&nbsp;end

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

-Kelly

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [7 March 2006 08:00 UTC](https://rubytalk.org/t/threads-and-exceptions/25498/2 "2006-03-07T08:00:53Z")

</div>

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

> **···**
>
> 2006/3/7, Kelly Felkins \<railsinator@gmail.com\>:
> 
> > 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.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.open(buffer[:file\_name], 'a') do |f|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.print(buffer[:text])  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;rescue Exception  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@logger.error("could not write message\n #{$!}\n file  
> > \>#{buffer[:file\_name]}\<\n message \>#{buffer[:text]}\<")  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > If the file is invalid I think the thread dies -- but I don't see a message  
> > anywhere.
> 
> --  
> Have a look: [Robert K. | Flickr](http://www.flickr.com/photos/fussel-foto/)
