Error handling with @parallel decorator

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Jan 18 02:10:46 EST 2016


On Monday 18 January 2016 17:15, Ankur Agrawal wrote:

> I am trying to catch Abort exception. When I use fabric's run(...) method,
> the host it tries to connect is not available and so it aborts with
> connect time out exception. I am not able to catch it. Following is a two
> different ways of code snippet-
> First I try following  -
> 
> class FabricException(Exception):
>     pass
> 
> with settings(abort_exception = FabricException):
> 
> try:
>     output = run(command)
> except FabricException:
>     print 'inside exception'
>     LOG.debug("inside exception")
> 
> It didn't go in exception block. Instead it threw -
> NetworkError: Timed out trying to connect to pqaltsnas300.corp.intuit.net
> (tried 1 time)
> Aborting.
> SystemExit: 1

Are you sure that the exception is being raised where you think it is being 
raised? You think that it is being raised by run(command), but it is 
possible that the exception is occurring somewhere else?

Don't catch the exception at all, and read the *entire* traceback. It will 
show you what line is raising the error.

Then, surround that line with:

try:
    the line that fails
except Exception as err:
    print err
    print type(err)
    LOG.debug(err)
    raise

This will tell you exactly what the exception type actually is. Perhaps you 
are trying to catch the wrong thing.

VERY IMPORTANT: catching all exceptions in this way should nearly always 
only be used for debugging purposes. Don't do that in production.

https://realpython.com/blog/python/the-most-diabolical-python-antipattern/




-- 
Steve




More information about the Python-list mailing list