异常可以理解为不可预期的不希望出现的动作或者操作。
异常处理防止程序因为某些意外中断,保证程序正常运行。
大的项目设计异常处理很重要。
异常处理:try/except
try:
do thing1
do thing2
......
except errortype1:
do something
except errortype2:
do something
......
other-doing 异常处理成功或者没有异常做动作
1.try里是我们要做的动作
2.可能会出现意外错误,错误类型如果在except中被捕获到则执行相应操作,否则程序中断退出
3.成功捕获异常,try里边异常之后的动作停止操作,但是try/except之后的语句还会继续操作
异常处理:try/finally
try:
do thing1
do thing2
......
except errortype1: expect为可选操作
do something
except errortype2:
do something
......
finally:
do something
other-doing 异常处理成功或者没有异常做动作
1.try里是我们要做的动作
2.可能会出现意外错误,异常没有被捕获或者没有异常异常处理(except)则执行完finally后中断退出
3.不论是否出现异常或者异常是否被处理,finally里的动作都会被执行
4.成功处理异常,try/except/finally之后的操作也会被执行
异常处理:try/except/else/finally
try:
do thing1
do thing2
.....
except errortype1 as othername: 更换异常的名字,仅存活于此except
do something
except (errortype2,errortype3): 捕获其中的任一异常信息
do something
.....
else: 有异常出现则不做动作,否则做动作
do something
finally: 一直做动作
do something
other-doing 异常处理成功或者没有异常做动作
产生异常or自定义异常raise和assert
class myerror(Exception):
pass
try:
do somthing
.....
rasie myerror() 【raise 系统类】
except myerror: 或者except Exception、except 系统类
do something
raise 这个会传递异常到高层处理器或者默认处理器,停止程序,打印错误info
1.所有异常类都是继承Exception
2.我们可以创建自己的异常类抛出或者直接抛出系统带的异常类
3.监视父类会捕获子类的异常【也可以监视自定义的或者系统的异常类,这样有局限性,建议监视父类】
3.异常传递raise会再次抛出异常到终端显示器
try:
do somthing
.....
assert ,
except Exception as my:
print(my) #print(my.args)
1.test为假才抛出异常信息
2.对应名为AssertionError的异常
try/except test
try:
a = [1, 2, 3][3] # # IndexError: list index out of range
print("can't print") # 异常之后的停止动作
sorted([3, '4', 5]) # TypeError: '<' not supported between instances of 'str' and 'int'
except IndexError: # 可以监视父类except Exception as my: print(my)
print("list index out of range")
except TypeError:
print("TypeError:")
print("the thing after try/except !", "1+2=%d" % (1+2))
list index out of range
the thing after try/except ! 1+2=3
try/except/finally test
try:
a = [1, 2, 3][3] # IndexError: list index out of range
print("can't print")
sorted([3, '4', 5]) # TypeError: '<' not supported between instances of 'str' and 'int'
except IndexError:
print("list index out of range")
except TypeError:
print("TypeError:")
finally: # 一直做动作
print("the thing in finally always can be done!")
print("the thing after try/except/finally !", "1+2=%d" % (1+2)) # 成功处理异常或者无异常做动作
list index out of range
the thing in finally always can be done!
the thing after try/except/finally ! 1+2=3
try/except/else/finally test
try:
a = [1, 2, 3][0] # [1, 2, 3][4]
sorted([3, 4, 5]) # [3,'4',5]
print("no error so print")
except (IndexError, TypeError) as my_define:
print(my_define) # "list index out of range or TypeError"
else: # 无异常做动作
print("no error so do else!")
finally: # 一直做动作
print("the thing in finally always can be done!")
print("the thing after try/except/else/finally !", "1+2=%d" % (1+2)) # 成功处理异常或者无异常做动作
no error so print
no error so do else!
the thing in finally always can be done!
the thing after try/except/else/finally ! 1+2=3
raise test
class mytype_error(Exception): # 自定义错误类
pass
try:
raise mytype_error("error_class_create_by_yourself")
except mytype_error as error_info: # 或者监视父类Exception
print(error_info)
print(error_info.args) # 以元组方式展示异常
try:
raise TypeError # 空信息&&抛出系统错误类并监视它
except TypeError as my:
print(my)
try:
raise TypeError("type_error") # 带有信息&&抛出系统错误类并监视它
except TypeError as my:
print(my)
try:
raise TypeError # 空信息&&监视父类exception
except Exception as my:
print(my)
try:
a = [1, 2, 3][4] # IndexError异常 监视的是父类exception,我们主动抛出的并"屏蔽"
raise TypeError("type_error")
except Exception as my:
print(my)
#raise 异常传递
error_class_create_by_yourself
('error_class_create_by_yourself',)
type_error
list index out of range
assert test
try:
assert (3 > 5), "123"
except AssertionError as my: # 或者监视父类-except Exception
print(my)
123