Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101870 > unrolled thread
| Started by | Ankur Agrawal <ankur.cse@gmail.com> |
|---|---|
| First post | 2016-01-18 06:15 +0000 |
| Last post | 2016-01-19 15:01 +0000 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Error handling with @parallel decorator Ankur Agrawal <ankur.cse@gmail.com> - 2016-01-18 06:15 +0000
Re: Error handling with @parallel decorator Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-01-18 18:10 +1100
Re: Error handling with @parallel decorator Ankur Agrawal <ankur.cse@gmail.com> - 2016-01-19 15:01 +0000
| From | Ankur Agrawal <ankur.cse@gmail.com> |
|---|---|
| Date | 2016-01-18 06:15 +0000 |
| Subject | Error handling with @parallel decorator |
| Message-ID | <mailman.85.1453097737.15297.python-list@python.org> |
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
Then I try to continue even if exception -
command = 'ls -l'
with settings(warn_only = True):
output = run(command)
Still it threw the same exception
NetworkError: Timed out trying to connect to pqaltsnas300.corp.intuit.net
(tried 1 time)
Aborting.
SystemExit: 1
Appreciate if somebody tell if I am missing anything ?
Thanks,
Ankur
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2016-01-18 18:10 +1100 |
| Message-ID | <569c8ff9$0$1523$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #101870 |
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
[toc] | [prev] | [next] | [standalone]
| From | Ankur Agrawal <ankur.cse@gmail.com> |
|---|---|
| Date | 2016-01-19 15:01 +0000 |
| Message-ID | <mailman.110.1453215715.15297.python-list@python.org> |
| In reply to | #101871 |
Thanks a lot Steven for your reply. I got the issue, it was my own
FabricException class, when I started using Exception then I could catch
the exception successfully and then I got the type of exception as well by
using your suggested type(err). Your code snippet did help me to find the
issue sooner.
Thanks,
Ankur
On Sun, Jan 17, 2016 at 11:16 PM Steven D'Aprano <
steve+comp.lang.python@pearwood.info> wrote:
> 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
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web