@songying
2018-12-30T13:49:32.000000Z
字数 602
阅读 1390
python
参考:内建异常的参考
raise Runtimeerror("something")
except ExceptionName as e:# some coderaise Runtimeerror("something")
try...except...try...except...else...try...except...else...finally...try...except...except...else...finally...try...finally...
try:正常代码except ErrorName1 as e:捕捉到异常后的代码except ErrorName2 as e:捕捉到异常后的代码else:无异常时执行的代码finally:无论有没有异常都需要执行的代码
BaseException是所有异常的父类,因此,此时可以使用BaseException来捕捉未知的异常。
try:正常代码except ErrorName1 as e:捕捉到异常后的代码except BaseException as e:捕捉到异常后的代码
注意:将
BaseException写到最后一个Exception中是最合适的方式。
创建自定义的异常类需要从 Exception 类继承
class ErrorName(Exception)
