getting with statement to deal with various exceptions

mk mrkafk at gmail.com
Sun Jul 20 19:23:24 CEST 2008


>    from __future__ import with_statement
> 
>    class ExceptionManager(object):
>        def __enter__(self):
>            pass
>        def __exit__(self,exc_type,exc_value,tb):
>            if exc_type == IOError:
>                print 'IOError',exc_value[1]
>                return True # suppress it
> 
>    with ExceptionManager():
>        with open('test.txt') as f:
>            f.read()

Neat. I think I'm going to use this approach, since BDFL-in-chief is 
unlikely to listen to me in any foreseeable future. :-)




More information about the Python-list mailing list