Except nest @ runtime

Alex Martelli aleax at aleax.it
Sun May 18 12:01:21 EDT 2003


JÿGius³ wrote:

> Hi All.
> 
> Given the following code...
> 
> def funcB():
>     print 10 + [] # ERRORE!!!!
> 
> def funcA():
>     try:
>         funcB()
>     except TypeError:
>         print 'errore interno'
> 
> try:
>     funcA()
> except TypeError:
>     print 'errore esterno'
> # output
> # errore interno
> 
> Yes, Python picks the most recent with a matching except, which in my case
> is try inside funcB. But what does it mean? Why this behaviour?

The "errore esterno" would be triggered only if an exception *PROPAGATED
OUT* from funcA (terminating funcA abnormally).  But it doesn't, of course:
you have a try/except inside funcA itself to STOP the TypeError exception
from propagating...!  So your questions are very mysterious to me.  Are
you asking why an except clause isn't triggered when no exception is
propagating to it?  Well that's the basic SEMANTICS of except clauses --
it's what they MEAN.  You might as well be asking why the body of an
if clause is executed when the condition is satisfied, or why 2+2 is 4...

If you prefer to use Italian for this discussion, I suggest you post to
it.comp.lang.python, which was recently opened and is quite active now.


Alex





More information about the Python-list mailing list