Superclass for Errors?

T

tac-tics

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.

Normally, I could simply use a regular

except:
....

but then I don't have access to the error message.

So what's the best solution to this problem?
 
C

Christian Joergensen

tac-tics said:
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.

See http://rgruet.free.fr/PQR25/PQR2.5.html#BuiltInExc

I would guess you're looking for StandardError.
 
C

Carsten Haese

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:
.... 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
 
G

Gabriel Genellina

At said:
For exceptions, I can simply use a catch-all except statement like:

try:
...
except Exception, error:
JOptionPane.showMessageDialog(self, "Error: %s" % error)

Normally, I could simply use a regular

except:
....

but then I don't have access to the error message.

Other people already said that all builtin exceptions are derived
from Exception. So using except Exception: xxx, you catch all of
them. What's left:
- string exceptions: deprecated long time ago, but you might
encounter them in old code.
- other classes not derived from Exception: still legal, probablly
not on future Python versions.
If you really have to catch any kind of exception, use a bare except
clause; you always can retrieve the exception details using
sys.exc_info()


--
Gabriel Genellina
Softlab SRL






__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top