Question on try/except

D

Dan

While perusing sre.py in the standard library, I came upon this snippet
of code in the _compile() function definition:

try:
p = sre_compile.compile(pattern, flags)
except error, v:
raise error, v # invalid expression

Is there some particular use in catching an exception and immediately
re-raising it? Why catch it at all?

/Dan
 
K

Kirk McDonald

Dan said:
While perusing sre.py in the standard library, I came upon this snippet
of code in the _compile() function definition:

try:
p = sre_compile.compile(pattern, flags)
except error, v:
raise error, v # invalid expression

Is there some particular use in catching an exception and immediately
re-raising it? Why catch it at all?

/Dan

All I can think of is that it changes the traceback to point to the
re-raise and not the original raise.

-Kirk McDonald
 
P

Patrick Maupin

Kirk said:
All I can think of is that it changes the traceback to point to the
re-raise and not the original raise.

I've used this technique before. It is very useful where (as in the
original re example) recursion might cause a large traceback, and the
error is probably caused by the caller's code. Using this technique
can keep the visible portion of the traceback relevant to the problem
at hand.

Regards,
Pat
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top