Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'else:': 0.03; 'elif': 0.05; 'attribute': 0.07; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'specific.': 0.09; 'things,': 0.09; 'try:': 0.09; 'python': 0.11; 'assume': 0.14; 'windows': 0.15; '3.3,': 0.16; 'blocks': 0.16; 'errno': 0.16; 'finney': 0.16; 'ioerror': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:exception': 0.16; 'exception': 0.16; 'import': 0.22; 'header:User-Agent:1': 0.23; '(by': 0.24; 'versions': 0.24; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'on,': 0.29; 'dos': 0.30; 'errors': 0.30; 'code': 0.31; 'exceptions': 0.31; 'ones.': 0.31; 'writes:': 0.31; 'class': 0.32; 'url:python': 0.33; 'subject:with': 0.35; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'add': 0.35; "i'll": 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'half': 0.37; 'operating': 0.37; 'skip:o 20': 0.38; 'system,': 0.38; 'ben': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'itself': 0.39; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'catch': 0.60; 'improved': 0.60; 'url:3': 0.61; "you're": 0.61; 'more': 0.64; 'latest': 0.67; 'distinguish': 0.84; 'subject:Using': 0.84; 'victor': 0.84; '\xe2\x80\xa6': 0.84; 'shell,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: Using "with" context handler, and catching specific exception? Date: Tue, 22 Oct 2013 14:53:58 +1100 References: <829a6777-3410-49ea-8652-ea1e4ceb9cd2@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: rasputin.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-gpg.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Cancel-Lock: sha1:WHXxaZPCsoQS686qpePKF/NA37M= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1382414051 news.xs4all.nl 15891 [2001:888:2000:d::a6]:48966 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:57231 Victor Hooi 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 , 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 and then distinguish the specific errors by their ‘errno’ attribute :: 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