工作环境:Python 3
利用try…except语句捕获异常
try:
......
excpet Exception as e:
print e
把 Exception
存成e,并且打印出来,这样就可以清楚地知道是为什么出bug了。
例如:
代码
def parse_int(s):
try:
n = int(v)
except Exception as e:
print("Couldn't parse")
print('Reason:', e)
实验
>>> parse_int('42')
Couldn't parse
Reason: global name 'v' is not defined
>>>