Superclass for Errors?

Carsten Haese carsten at uniqsys.com
Wed Dec 27 15:37:59 EST 2006


On Wed, 2006-12-27 at 12:13 -0800, tac-tics wrote:
> I have a program which has a GUI front-end which that runs a separate
> thread to handle all the important stuff. However, if there is a
> problem with the important stuff, I want the GUI to raise a MessageBox
> alert to indicate this.
> 
> For exceptions, I can simply use a catch-all except statement like:
> 
> try:
>     ...
> except Exception, error:
>     JOptionPane.showMessageDialog(self, "Error: %s" % error)
> 
> Only, I want it to catch Errors as well. Right now, I'm using:
> 
> try:
>     ...
> except (Exception, TypeError, NameError, RuntimeError, AttributeError),
> error:
>     JOptionPane.showMessageDialog(self, "Error: %s" % error)
> 
> I was wondering if there is a superclass for TypeError, NameError,
> RuntimeError, AttributeError, etc.

Yes, that superclass is Exception:

>>> for klass in (TypeError, NameError, RuntimeError, AttributeError):
...   print klass, issubclass(klass, Exception)
...
exceptions.TypeError True
exceptions.NameError True
exceptions.RuntimeError True
exceptions.AttributeError True

Have you encountered many TypeError, NameError, RuntimeError or
AttributeError exceptions that "except Exception" by itself failed to
catch?

-Carsten





More information about the Python-list mailing list