Catching a pywintypes.api_error excpeption?

Peter Hansen peter at engcorp.com
Fri Aug 2 07:49:39 EDT 2002


Derek Basch wrote:
> 
> Hi,
> Can someone tell me how to catch this exception with a
> try, except clause:
> 
>     SetCommState(self.handle, dcb)
> pywintypes.api_error: (87, 'SetCommState', 'The
> parameter is incorrect.')
> 
> every exception I have previously worked with has been
> a class such as IOError. How do i catch this one?

As with "except IOError", just use the name of the class:

try:
    SetCommState(self.handle, dcb)
except pywintypes.api_error:
    print 'spam spam spam spam'

(Although this sounds more like an exception coming from
a bug, not a runtime condition that you would want to catch
like an IOError.)

-Peter



More information about the Python-list mailing list