Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109444 > unrolled thread
| Started by | Piyush Verma <114piyush@gmail.com> |
|---|---|
| First post | 2016-06-04 04:44 +0530 |
| Last post | 2016-06-04 16:39 -0700 |
| Articles | 4 — 4 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.
Catch exception with message? Piyush Verma <114piyush@gmail.com> - 2016-06-04 04:44 +0530
Re: Catch exception with message? Steven D'Aprano <steve@pearwood.info> - 2016-06-04 12:38 +1000
Re: Catch exception with message? Peter Pearson <pkpearson@nowhere.invalid> - 2016-06-04 16:44 +0000
Re: Catch exception with message? Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-04 16:39 -0700
| From | Piyush Verma <114piyush@gmail.com> |
|---|---|
| Date | 2016-06-04 04:44 +0530 |
| Subject | Catch exception with message? |
| Message-ID | <mailman.138.1464995679.1839.python-list@python.org> |
Generally we catch exception using except Exception as e: But sometimes, we see same type of exception is present with different message.Is there a way to capture same exception with message filtering? Please help me to do this. Regards, ~Piyush
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-06-04 12:38 +1000 |
| Message-ID | <57523f1f$0$1587$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #109444 |
On Sat, 4 Jun 2016 09:14 am, Piyush Verma wrote:
> Generally we catch exception using
> except Exception as e:
>
> But sometimes, we see same type of exception is present with different
> message.
That suggests that whoever wrote the code doesn't know what they're doing.
Intentionally giving the same error message for different exception types
is poor design.
> Is there a way to capture same exception with message
> filtering? Please help me to do this.
Error messages are not part of the public API of Python's built-ins and
standard library. That means that error messages are subject to change at
any time, without warning. There is no backwards compatibility requirement
for the error message to stay the same.
In principle that means that the same function might use a different error
message each time you run it. But in practice, it means that even bug-fix
releases of Python can change error messages.
So don't do this, it is a terrible idea. You should never rely on the error
message of an exception.
But if you insist:
try:
something()
except Exception as e:
if e.args[0] = "An error occurred":
print("could you be any less specific?")
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Peter Pearson <pkpearson@nowhere.invalid> |
|---|---|
| Date | 2016-06-04 16:44 +0000 |
| Message-ID | <drgeqkFcclcU1@mid.individual.net> |
| In reply to | #109444 |
On Sat, 4 Jun 2016 04:44:30 +0530, Piyush Verma <114piyush@gmail.com> wrote: > Generally we catch exception using > except Exception as e: > > But sometimes, we see same type of exception is present with different > message.Is there a way to capture same exception with message > filtering? Please help me to do this. Just to make sure, . . . are you aware that "except Exception" is generally not what you want to do, that you generally want to say "except ValueError" or "except KeyError" or "except UnicdeDecodeError" or something specific like that? Maybe that's where to find the specificity you seek. -- To email me, substitute nowhere->runbox, invalid->com.
[toc] | [prev] | [next] | [standalone]
| From | Lawrence D’Oliveiro <lawrencedo99@gmail.com> |
|---|---|
| Date | 2016-06-04 16:39 -0700 |
| Message-ID | <15db1dce-b144-496a-9ed0-8e3ebf447f9c@googlegroups.com> |
| In reply to | #109483 |
On Sunday, June 5, 2016 at 4:44:17 AM UTC+12, Peter Pearson wrote: > ... are you aware that "except Exception" is generally not what you want to > do ... Agree. Always make your “except”-matching as specific as possible.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web