Python异常

阅读 48

2022-07-12

异常捕获语法

try:
<语句>
except <名字>:
<语句>

抛出异常

raise Exception

异常表
Python异常_异常捕获

捕获多分异常

def mult_exception(x,y):
try:
a = x/y
b = name
except ZeroDivisionError:
print('this is ZeroDivisionError')
except NameError:
print('this is NameError')

mult_exception(2,0)

统一捕获异常

def model_exception(x,y):
try:
a = x/y
b = name
except (ZeroDivisionError, NameError, TypeError) as e:
print(e)

model_exception(2,'')

finally

def use_finally(x,y):
try:
a = x/y
finally:
print('No matter what happened,I will show in front of you')

use_finally(2,0)


精彩评论(0)

0 0 举报