如何获取默认情况下抛出的异常对象

时间:2021-07-18 20:27:49

I know that we can get the exception object if we use the 'as' syntax:-

我知道如果我们使用'as'语法,我们可以得到异常对象: -

try:
    1/0
except ZeroDivisionError as e:
    print "can not divide zero"
    print(str(e))

I'd like to capture everything .... ex:-

我想抓住一切......前: -

try:
    1/0
except * as e:
    print "some error"
    print(str(e))

Can that be done?

可以这样做吗?

2 个解决方案

#1


All of Python's exceptions are subclasses of Exception, so you want:

所有Python的异常都是Exception的子类,所以你想要:

try:
    1/0
except Exception as e:
    print "some error"
    print(str(e))
# Output:
some error
integer division or modulo by zero

#2


Catch Exception like

像Catch Exception一样

except Exception, e:

#1


All of Python's exceptions are subclasses of Exception, so you want:

所有Python的异常都是Exception的子类,所以你想要:

try:
    1/0
except Exception as e:
    print "some error"
    print(str(e))
# Output:
some error
integer division or modulo by zero

#2


Catch Exception like

像Catch Exception一样

except Exception, e: