Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #57231

Re: Using "with" context handler, and catching specific exception?

From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Using "with" context handler, and catching specific exception?
Date 2013-10-22 14:53 +1100
References <a2623010-97d3-4a6d-9373-e9979492b84f@googlegroups.com> <mailman.1331.1382411066.18130.python-list@python.org> <829a6777-3410-49ea-8652-ea1e4ceb9cd2@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1334.1382414051.18130.python-list@python.org> (permalink)

Show all headers | View raw


Victor Hooi <victorhooi@gmail.com> writes:

> Aha, good point about IOError encapsulating other things, I'll use
> FileNotFoundError, and also add in some other except blocks for the
> other ones.

Or not; you can catch OSError, which is the parent of FileNotFoundError
<URL:http://docs.python.org/3/library/exceptions.html#exception-hierarchy>,
but don't assume in your code that it means anything more specific.

You should only catch specific exceptions if you're going to do
something specific with them. If all you want to do is log them and move
on, then catch a more general class and ask the exception object to
describe itself (by using it in a string context).


In versions of Python before 3.3, you have to catch EnvironmentError
<URL:http://docs.python.org/3.2/library/exceptions.html#EnvironmentError>
and then distinguish the specific errors by their ‘errno’ attribute
<URL:http://docs.python.org/3.2/library/errno.html>::

    import errno

    try:
        with open('somefile.log', 'wb') as f:
            f.write("hello there")
    except EnvironmentError as exc:
        if exc.errno == errno.ENOENT:
            handle_file_not_found_error()
        elif exc.errno == errno.EACCES:
            handle_permission_denied()
        elif exc.errno == errno.EEXIST:
            handle_file_exists()
        …
        else:
            handle_all_other_environment_errors()

That's much more clumsy, which is why it was improved in the latest
Python. If you can, code for Python 3.3 or higher.

-- 
 \     “Unix is an operating system, OS/2 is half an operating system, |
  `\    Windows is a shell, and DOS is a boot partition virus.” —Peter |
_o__)                                                        H. Coffin |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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