From: anselm Date: 2007-03-22T20:35:05+09:00 Subject: Re: Catching thread exceptions (Net::IMAP timeout) On Mar 21, 9:03 pm, Brian Candler wrote: > On Thu, Mar 22, 2007 at 02:55:12AM +0900, anselm wrote: > > I have a question : is it possible to catch exceptions that are raised > > on a thread by another thread ? Say if you have two threads, A and B > > and thread A does > > > B.raise "bug" > > > Is there a way for thread B to catch and process that exception, and > > resume it's execution where it was at ? > > Well, one option is for B to start a new thread C, where it does all its > work. The original thread B hangs around in an infinite loop just catching > and processing exceptions. Yes, this is pretty good thank you ! Maybe in my particular case I could do it the other way round : start the imap thread from a new thread wich will then just wait for the exception. This should then re- raise exceptions that are not the timeout back on the main thread (since in those cases the imap library blocks while waiting for responses, this should not be a problem -- the exception should still appear to come out of the imap call) mainthread = Thread.current imap = nil Thread.new { begin limap = Net::IMAP.new('example.com') limap.login('username', 'password') imap = limap sleep 10000 while true rescue Exception => e if e.kind_of? Net::IMAP::ByeResponseError # deal with timeout else mainthread.raise e end end } # Wait for connection to be established begin sleep 1 while imap.nil? rescue Exception => e # Connection error end begin imap.select('', 'INBOX') rescue Net::IMAP::Error # Synchronous exceptions should still be caught here end Thanks, Anselm