Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #92005
| References | <87egltht87.fsf@Equus.decebal.nl> <mailman.108.1433338144.13271.python-list@python.org> <876174ix9n.fsf@Equus.decebal.nl> |
|---|---|
| Date | 2015-06-04 08:00 +1000 |
| Subject | Re: Retrying to send message |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.134.1433368816.13271.python-list@python.org> (permalink) |
On Thu, Jun 4, 2015 at 2:15 AM, Cecil Westerhof <Cecil@decebal.nl> wrote:
>> And I'd also skip the bare except clause. If you get any sort of
>> exception, whether it's a bug, a failure from libturpial, a network
>> error, or anything else, your code will just terminate with a bland
>> and useless message. Much better to simply let the exception bubble
>> up.
>
> I kept the except. I like to see the message that went wrong. ;-)
In that case, there's an easier way to deal with it: just raise a
different exception, and let them chain. Something like this:
>>> def send_message(msg):
... try: 1/0
... except: raise FailedMessageException(msg)
...
>>> class FailedMessageException(Exception): pass
...
>>> send_message("Test")
Traceback (most recent call last):
File "<stdin>", line 2, in send_message
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in send_message
__main__.FailedMessageException: Test
But I'd still avoid the bare except, and use "except Exception:"
instead. I don't think you want to catch SystemExit in this way, for
instance. So here's how I'd write that code:
>>> def send_message(msg):
... try: 1/0
... except Exception as e: raise FailedMessageException(msg) from e
...
>>> send_message("Test")
Traceback (most recent call last):
File "<stdin>", line 2, in send_message
ZeroDivisionError: division by zero
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in send_message
__main__.FailedMessageException: Test
(Also, the use of "from" here causes a slightly different wording,
which I think is more appropriate. But that's minor.)
You get the report of which message failed, plus you get the complete
traceback from the original exception. Much MUCH more useful.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Retrying to send message Cecil Westerhof <Cecil@decebal.nl> - 2015-06-03 14:27 +0200
Re: Retrying to send message Chris Angelico <rosuav@gmail.com> - 2015-06-03 23:29 +1000
Re: Retrying to send message Cecil Westerhof <Cecil@decebal.nl> - 2015-06-03 18:15 +0200
Re: Retrying to send message MRAB <python@mrabarnett.plus.com> - 2015-06-03 19:12 +0100
Re: Retrying to send message Ethan Furman <ethan@stoneleaf.us> - 2015-06-03 11:28 -0700
Re: Retrying to send message Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-03 21:37 +0100
Re: Retrying to send message Chris Angelico <rosuav@gmail.com> - 2015-06-04 08:00 +1000
Re: Retrying to send message Ethan Furman <ethan@stoneleaf.us> - 2015-06-03 16:15 -0700
Re: Retrying to send message Cecil Westerhof <Cecil@decebal.nl> - 2015-06-04 07:13 +0200
csiph-web