By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

Superclass for Errors?

P: n/a
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?

Dec 27 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"tac-tics" <ta*******@gmail.comwrites:
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.

--
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk | Visit us at: http://www.gmta.info
Dec 27 '06 #2

P: n/a
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
Dec 27 '06 #3

P: n/a
At Wednesday 27/12/2006 17:13, tac-tics wrote:
>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

Dec 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.