异常类型集合
实战
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2021/8/17 21:54
# @Author : InsaneLoafer
# @File : try_type.py
class Test(object):
pass
t = Test()
try:
t.name
except AttributeError as e:
print(e)
d = {'name': 'insane'}
try:
d['age']
except KeyError as e:
print('没有对应的键:', e)
l = [1, 2, 3]
try:
l[4]
except IndexError as e:
print(e)
name = 'insane'
try:
int(name)
except ValueError as e:
print(e)
def test(a):
return a
try:
test()
except TypeError as e:
print(e)
'Test' object has no attribute 'name'
没有对应的键: 'age'
list index out of range
invalid literal for int() with base 10: 'insane'
test() missing 1 required positional argument: 'a'
Process finished with exit code 0