Awsome Python - chained exceptions

Serhiy Storchaka storchaka at gmail.com
Fri Feb 15 19:51:21 CET 2013


On 14.02.13 08:39, Steven D'Aprano wrote:
> Here is one example of using raise to re-raise an exception you have just
> caught:
>
> import errno
> paths = ["here", "there", "somewhere else"]
> for location in paths:
>      filename = os.path.join(location, "prefs.ini")
>      try:
>          f = open(filename)
>      except IOError as e:
>          if e.errno != errno.ENOENT:  # File not found.
>              raise
>

In Python 3.3:

     try:
         f = open(filename)
     except FileNotFound:
         pass

But not all errnos have special OSError subclasses.





More information about the Python-list mailing list