Catching Exceptions.

K

Keith Bolton

I am handling exceptions currently using try, except. Generally I don't
handle specific exceptions and am catching all.
Then if an exception occurs, I would like to capture that error string.
However, in the documentation it seems like
there is not a way to get the extra str data if you are handling all
exceptions and not specifically. Is there?

thanks

-keith
 
P

Peter Hansen

Keith said:
I am handling exceptions currently using try, except. Generally I don't
handle specific exceptions and am catching all.
Then if an exception occurs, I would like to capture that error string.
However, in the documentation it seems like
there is not a way to get the extra str data if you are handling all
exceptions and not specifically. Is there?

Do generic "try/except" is a *very* bad idea, and almost never
either required, or hard to avoid.

Nevertheless, if you are doing it, you can basically just
do this to get the extra info:

try:
# failing code
except Exception, ex:
# use ex here just as you would have in a more specific handler
# such as with str(ex)

The key is that all exceptions are descendents of the class
Exception, except *deprecated* exceptions that are just
strings (as in "raise 'somefailure'"). (And those should never
be used in your own code anyway.)

-Peter
 
F

Fuzzyman

I am handling exceptions currently using try, except. Generally I don't
handle specific exceptions and am catching all.
Then if an exception occurs, I would like to capture that error string.
However, in the documentation it seems like
there is not a way to get the extra str data if you are handling all
exceptions and not specifically. Is there?

thanks

-keith

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259180

Above link is a simple recipe that will just extract the text of an
exception.

HTH

Regards,


Fuzzy

http://www.voidspace.org.uk/atlantibits/pythonutils.html
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top