Identifying exceptions

Harri Pasanen (pa@tekla.fi)
07 Mar 1995 12:06:52 GMT

Hello,

Let's presume I have the following piece of code:

from socket import *
from struct import *

try:
something_with_struct_and_sockets
except: # Dilemma, how to catch only struct and socket exceptions?
...

Now both struct and and socket have the error attribute. Because
struct is imported after socket, its 'error' attribute overrides
'error' from socket.

Possible solutions:

A)

from socket import *
import struct

try:
something_with_struct_and_sockets
except (error, struct.error), msg:
print msg

B)

from socket import *
SocketError = error
from struct import *
StructError = error

try:
something_with_struct_and_sockets
except (SocketError,StructError), msg:
print msg

This is kind of hacky, but it works.

Are there other, better solutions?

----------------------------------------------------------------------------

The underlying problem is that if I use:

from module import *

or

import * from module

Then the generated exception is dependent on which import method
was used. I'd like a module error to be 'module.error', independent
of the import statements, but I'm not sure if this would clash with
semantics of exceptions in general.

Related question, is there a way to get exception's parameters
without knowing which exception was raised?

Thanks,

Harri

--
==========================================================================
Harri Pasanen   pa@tekla.fi