Python中的异常处理有多种方式,至于区别,望各位亲测
方式一
print("begin>>>>>>>>>>>>>>>>>>>>")
try:
print("this is a string " + 3)
except:
print("【error】")
print("end>>>>>>>>>>>>>>>>>>>>")
方式二
print("begin>>>>>>>>>>>>>>>>>>>>")
try:
print("this is a string " + 3)
except Exception as r:
print("【error】 %s" %(r))
print("end>>>>>>>>>>>>>>>>>>>>")
方式三
import sys
print("begin>>>>>>>>>>>>>>>>>>>>")
try:
print("this is a string " + 3)
except:
errMsg = sys.exc_info()
print(errMsg)
for item in errMsg:
print(item )
print("end>>>>>>>>>>>>>>>>>>>>")
方式四
import traceback
print("begin>>>>>>>>>>>>>>>>>>>>")
try:
print("this is a string " + 3)
except:
traceback.print_exc()
print("end>>>>>>>>>>>>>>>>>>>>")