traceback.print_exc() supposed to stop exception propagation.

Diez B. Roggisch deets at nospam.web.de
Sun Apr 6 21:58:48 CEST 2008


Sami schrieb:
> Hello,
> 
> In the Python book that I am using to learn the language it says that 
> the traceback.print_exc() can be used to stop exception propagation and 
> make the program keep running.
> 
> Here is a simple piece of code that I typed in to test this fact:
> ---------------------------------------------------------------------------
> import sys
> 
> def Myexcepthook(etype, value, tb):
>     print "in Myexcepthook\n"
>     import traceback
>     lines=traceback.format_exception(etype, value, tb)
>     print "\n".join(lines)
>     traceback.print_exc()
> 
> 
> sys.excepthook = Myexcepthook
> 
> x = 1/0
> 
> x = 78
> 
> print x
> --------------------------------------------------------------------------
> The Output:
> --------------------------------------------------------------------------
> in Myexcepthook
> 
> Traceback (most recent call last):
> 
>   File 
> "E:\Home\Programming\Python\TryProjects\ExceptHandling1\Except2.py", lin
>  15, in <module>
>     x = 1/0
> 
> ZeroDivisionError: integer division or modulo by zero
> 
> None
> --------------------------------------------------------------------------
> 
> I never see the value 78.
> 
> What am I doing wrong?

Trusting a wrong source. Or misinterpreting it.

Python 2.5.1 (r251:54863, May  2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
imWelcome to rlcompleter2 0.96
for nice experiences hit <tab> multiple times
 >>> import traceback
 >>> help(traceback.print_exc)
Help on function print_exc in module traceback:

print_exc(limit=None, file=None)
     Shorthand for 'print_exception(sys.exc_type, sys.exc_value, 
sys.exc_traceback, limit, file)'.
     (In fact, it uses sys.exc_info() to retrieve the same information
     in a thread-safe way.)

 >>>

Nothing in there says that this would prevent the exception from being 
propagated.

Diez



More information about the Python-list mailing list