Syntax to catch specific exceptions ?

pekka niiranen krissepu at vip.fi
Mon Mar 11 02:55:17 EST 2002


Thanks Fredrik,

So I will end up writing function with a huge "if-else"-structure under 
except -clause
that catches all cases I want react to? Sounds like C. Perkele ;)

By the way, have you had time to consider re.findallX -function that
does not return empty matches ?
   
-pekka-

Fredrik Lundh wrote:

>Pekka Niiranen wrote:
>
>>I can get the error text and number as follows, but how to catch only
>>"File exists" OSError ?
>>
>>try:
>>...  os.mkdir('e:\\tmp')
>>except OSError, (errno, strerror):
>>...  print strerror
>>...
>>File exists
>>
>
>catch it, check the errno attribute, and raise it again
>if it's not the right error:
>
>import os, errno
>
>try:
>    os.mkdir(somedir)
>except OSError, v:
>    if v.errno != errno.EEXIST:
>        raise
>    # directory exists
>
>alternatively, you can use os.path.isdir to check if the
>directory exists before you call mkdir.
>
></F>
>
><!-- (the eff-bot guide to) the python standard library:
>http://www.pythonware.com/people/fredrik/librarybook.htm
>-->
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20020311/67ceb48f/attachment.html>


More information about the Python-list mailing list