Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #57227
| X-Received | by 10.66.248.193 with SMTP id yo1mr7673807pac.6.1382412426702; Mon, 21 Oct 2013 20:27:06 -0700 (PDT) |
|---|---|
| X-Received | by 10.50.73.99 with SMTP id k3mr118650igv.9.1382412426288; Mon, 21 Oct 2013 20:27:06 -0700 (PDT) |
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news.snarked.org!newsfeed.news.ucla.edu!usenet.stanford.edu!z6no56764035pbz.1!news-out.google.com!rn2ni116662pbc.1!nntp.google.com!i2no26165192qav.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.python |
| Date | Mon, 21 Oct 2013 20:27:05 -0700 (PDT) |
| In-Reply-To | <mailman.1331.1382411066.18130.python-list@python.org> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=150.101.201.178; posting-account=LMx4fAoAAAAL3v616YFvkt1ueXc1H63- |
| NNTP-Posting-Host | 150.101.201.178 |
| References | <a2623010-97d3-4a6d-9373-e9979492b84f@googlegroups.com> <mailman.1331.1382411066.18130.python-list@python.org> |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <829a6777-3410-49ea-8652-ea1e4ceb9cd2@googlegroups.com> (permalink) |
| Subject | Re: Using "with" context handler, and catching specific exception? |
| From | Victor Hooi <victorhooi@gmail.com> |
| Injection-Date | Tue, 22 Oct 2013 03:27:06 +0000 |
| Content-Type | text/plain; charset=windows-1252 |
| Content-Transfer-Encoding | quoted-printable |
| Xref | csiph.com comp.lang.python:57227 |
Show key headers only | View raw
Hi,
Thanks for the replies =).
Aha, good point about IOError encapsulating other things, I'll use FileNotFoundError, and also add in some other except blocks for the other ones.
And yes, I didn't use the exception object in my sample - I just sort. I'd probably be doing something like this.
logger.error("Some error message - %s" % e)
So is the consensus then that I should wrap the "with" in a try-except block?
try:
with open('somefile.log', 'wb') as f:
f.write("hello there")
except FileNotFoundError as e:
logger.error("Uhoh, the file wasn't there - %s" % e)
Cheers,
Victor
On Tuesday, 22 October 2013 14:04:14 UTC+11, Ben Finney wrote:
> Victor Hooi <victorhooi@gmail.com> writes:
>
>
>
> > try:
>
> > with open('somefile.log', 'wb' as f:
>
> > f.write("hello there")
>
> > except IOError as e:
>
> > logger.error("Uhoh, the file wasn't there").
>
>
>
> IOError, as Steven D'Aprano points out, is not equivalent to “file not
>
> found”. Also, you're not doing anything with the exception object, so
>
> there's no point binding it to the name ‘e’.
>
>
>
> What you want is the specific FileNotFoundError:
>
>
>
> try:
>
> with open('somefile.log', 'wb' as f:
>
> f.write("hello there")
>
> except FileNotFoundError:
>
> logger.error("Uhoh, the file wasn't there").
>
>
>
> See <URL:http://docs.python.org/3/library/exceptions.html#FileNotFoundError>.
>
>
>
> --
>
> \ “Choose mnemonic identifiers. If you can't remember what |
>
> `\ mnemonic means, you've got a problem.” —Larry Wall |
>
> _o__) |
>
> Ben Finney
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Using "with" context handler, and catching specific exception? Victor Hooi <victorhooi@gmail.com> - 2013-10-21 18:43 -0700
Re: Using "with" context handler, and catching specific exception? MRAB <python@mrabarnett.plus.com> - 2013-10-22 03:04 +0100
Re: Using "with" context handler, and catching specific exception? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-22 02:48 +0000
Re: Using "with" context handler, and catching specific exception? Ben Finney <ben+python@benfinney.id.au> - 2013-10-22 14:04 +1100
Re: Using "with" context handler, and catching specific exception? Victor Hooi <victorhooi@gmail.com> - 2013-10-21 20:27 -0700
Re: Using "with" context handler, and catching specific exception? Ben Finney <ben+python@benfinney.id.au> - 2013-10-22 14:53 +1100
Re: Using "with" context handler, and catching specific exception? Victor Hooi <victorhooi@gmail.com> - 2013-10-22 18:11 -0700
csiph-web