0
点赞
收藏
分享

微信扫一扫

Python异常

异常捕获语法

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 条评论