捕获所有异常并且打印

工作环境: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
>>>

引用:Python Cookbook

发布了22 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/helloworld987456/article/details/92796052