10.Python异常处理

10.1 Python标准异常类




10.2 实验

In:

a = 1
print(a + "输出")

out:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-1-6e1f6d473de4> in <module>
      1 a = 1
----> 2 print(a + "输出")
TypeError: unsupported operand type(s) for +: 'int' and 'str'

In:

try:
    a = 1
    print(a + "输出")
except Exception as e:
    print(f'{a}类型不是字符,错误代码:{e}')

out:

1类型不是字符,错误代码:unsupported operand type(s) for +: 'int' and 'str'

In:

def prt_info(info):
    try:
        print(info + "输出")
    except Exception as e:
        print(f'{info}类型不是字符,错误代码:{e}')
    else:
        print("正常")

In:

prt_info(1)

out:

1类型不是字符,错误代码:unsupported operand type(s) for +: 'int' and 'str'

In:

prt_info('a')

out:

a输出
正常

In:

def prt_info2(info):
    try:
        print(info + "输出")
    except Exception as e:
        print(f'{info}类型不是字符,错误代码:{e}')
    finally:
        print("执行完成")

In:

prt_info2(1)

out:

1类型不是字符,错误代码:unsupported operand type(s) for +: 'int' and 'str'
执行完成

In:

prt_info2('a')

out:

a输出
执行完成
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值