By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

traceback.print_exc() supposed to stop exception propagation.

P: n/a
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\ExceptHand ling1\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?

Thanks,
Sami
Apr 6 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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\ExceptHand ling1\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 <tabmultiple 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
Apr 6 '08 #2

P: n/a

"Sami" <sa********@NOSPAMbtinternet.comwrote in message
news:69******************************@bt.com...
| 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.

It is possible that the unspecified book describes an unspecified Python
version that is not the same as the unspecified version that you tested
with ;-).

Help respondants by providing version info. Sometimes even the system/OS
info is helpful, though probably not relevant here.

tjr

Apr 6 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.