[BangPypers] Nested try-catch

steve steve at lonetwin.net
Wed Oct 7 06:43:06 CEST 2009


Hi Anand,
On 10/06/2009 09:24 PM, Anand Chitipothu wrote:
> I encountered a tricky situation with nested try-catch. Here is the sample code:
> I managed to work-around that problem like this:
> [...snip...]
> try:
>      f()
> except:
>      x = sys.exc_info()
>      # do cleanup
>      try:
>          g()
>      except:
>          pass # ignore
>      raise x[0], x[1], x[2]
>
> It provides exception class, object and traceback to raise instead of
> letting raise take the last exception/traceback.
>
> Has anybody else faced similar situation? Any better solutions?
>

Well, i am wondering why you are extracting the exc_info() rather than saving 
off the exception object itself:
try:
     f()
except Exception, e
     saved = e
     try:
         g()
     except:
         pass # ignore
     raise e

I have not tested this on 3.0 but i am assuming this should work. If it doesn't 
try using finally:

try:
     f()
except Exception, e
     saved = e
     try:
         g()
     except:
         pass # ignore
     finally:
         raise e

cheers,
- steve
-- 
random non tech spiel: http://lonetwin.blogspot.com/
tech randomness: http://lonehacks.blogspot.com/
what i'm stumbling into: http://lonetwin.stumbleupon.com/


More information about the BangPypers mailing list