【发布时间】:2017-10-23 16:11:23
【问题描述】:
#!/usr/bin/env python
#import re
def test_ex1(input):
if re.match(r'^[A-Z]+$',input):
print 'Match'
return True
print 'No Match'
return False
#test_ex1('ABC')
try:
test_ex1('ABC')
except Exception:
raise Exception
如果我运行上述程序,它将打印以下异常消息。
a:~/Python> python test.py
Traceback (most recent call last):
File "test.py", line 18, in <module>
raise Exception
Exception
在不更改 test_ex1 子例程的情况下使用try except 捕获异常时,在 Python 中打印以下堆栈跟踪的正确方法是什么?
Traceback (most recent call last):
File "test.py", line 15, in <module>
test_ex1('ABC')
File "test.py", line 8, in test_ex1
if re.match(r'^[A-Z]+$',input):
NameError: global name 're' is not defined
【问题讨论】:
-
是否要重新引发异常?
-
如何递归打印所有异常而不在函数内的每个函数中添加
print_exc调用?