Exception question

R

Ron Adam

I'm trying to understand exception handling better and have a question I
haven't been able to find an answer too. Which probably means It won't
work, but...

Do exceptions that take place get stored in a stack or list someplace?

For example in:

try:
try:
try:
riskyfunc()
# error 1
except:
pass
crazyclass()
# error 2
except:
pass
iffycalc()
# error 3
except:
pass

# print any errors that occurred here.


If errors 1,2,and 3 are possible errors, can I get
a list of them, or can I only see the last one?

I know I can catch the error and store it myself with,

except Exception, exc:

or possibly,

errlist = []
errlist.append(sys.exc_info())

But what I want to know is does the interpreter do that in any way? And
if so, can I access it?

Cheers,
_Ron_Adam
 
S

Steven Bethard

Ron said:
Do exceptions that take place get stored in a stack or list someplace? [snip]
I know I can catch the error and store it myself with,

except Exception, exc:

or possibly,

errlist = []
errlist.append(sys.exc_info())

But what I want to know is does the interpreter do that in any way? And
if so, can I access it?

No, but there's been some recent talk about having it do so:

http://mail.python.org/pipermail/python-dev/2005-May/053672.html

If that happens though, it's not likely to show up before Python 3.0.
Too backwards incompatible to do now. It also seems that the exact
semantics haven't yet been agreed upon.

STeVe
 
R

Ron Adam

Steven said:
Ron said:
Do exceptions that take place get stored in a stack or list someplace?
[snip]

I know I can catch the error and store it myself with,

except Exception, exc:

or possibly,

errlist = []
errlist.append(sys.exc_info())

But what I want to know is does the interpreter do that in any way? And
if so, can I access it?


No, but there's been some recent talk about having it do so:

http://mail.python.org/pipermail/python-dev/2005-May/053672.html

If that happens though, it's not likely to show up before Python 3.0.
Too backwards incompatible to do now. It also seems that the exact
semantics haven't yet been agreed upon.

STeVe

Thanks Steve,

I had actually skimmed over those, but wasn't thinking of this at the
time. I reread them. And it clears up a few things.

I had read somewhere that exception objects were global, but that wasn't
correct, after testing it, they appear to be part of the local frame.
So once a function exits, any exceptions objects that happened inside
the function are no longer retrievable.

And checking exception ID's to see if any new exceptions occurred
doesn't work either, as the numbers get reused too frequently in a short
period.

Conclusion: Exceptions need to be logged or handled as they occur.

Cheers,
_Ron
 
S

Steven Bethard

Ron said:
I had read somewhere that exception objects were global, but that wasn't
correct, after testing it, they appear to be part of the local frame. So
once a function exits, any exceptions objects that happened inside the
function are no longer retrievable.

And checking exception ID's to see if any new exceptions occurred
doesn't work either, as the numbers get reused too frequently in a short
period.

Also, in case you don't get notification of PEP checkins, you'll want to
know that there's a new PEP about this:

PEP 344: Exception Chaining and Embedded Tracebacks [1]

STeVe

[1] http://www.python.org/peps/pep-0344.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

No members online now.

Forum statistics

Threads
474,239
Messages
2,571,200
Members
47,838
Latest member
elibuskamoSeAve

Latest Threads

Top