test1.py中,当分母为0的时候,调用系统退出
#!/usr/bin/python import sys def division(a=1, b=1): if b==0: print 'b eq 0' sys.exit(1) else: return a/b
test2.py中,用try..except捕获异常,然后traceback.print_exc()打印。
#!/usr/bin/python import sys import traceback import test1 a=10 b=0 try: print test1.division(a,b) except: print 'invoking division failed.' traceback.print_exc() sys.exit(1)
运行test2.py失败抛出异常。
$python test2.py
execution python-2.5.1/python (enodeb/linux)
b eq 0
invoking division failed.
Traceback (most recent call last):
File "test2.py", line 10, in <module>
test1.division(a,b)
File "/home/fesu/test1.py", line 6, in division
sys.exit(1)
SystemExit: 1