Python异常-try、raise语句及自定义异常类

2021-02-03 19:14

阅读:649

标签:erro   自定义异常   自定义   ISE   异常类   div   处理   on()   nbsp   

一、异常处理语句

#try/except
try:
    pass
except:
    pass

try:
    pass
except Exception:
    pass

#try/except ...
try:
    pass
except (Exception1, Exception2):
    pass

try:
    pass
except Exception1:
    pass
except Exception2:
    pass

#try/except ... as ...
try:
    pass
except Exception as e:
    isinstance(e, Exception)
except ...

try:
    pass
except (Exception1, Exception2) as e:
    print(e)

#try/except/else
try:
    pass
except ...:
    pass
except ...
else:
    pass
    
#try/finally
try:
    pass
finally:
    pass

try:
    pass
except ...:
    pass
finally:
    pass
    
try:
    pass
except ...:
    pass
except ...
else:
    pass
finally:
    pass

二、自定义异常类

class MyException(Exception):
    pass

三、显式触发异常

raise
raise MyException
raise MyException()
raise MyException(Error)
raise MyException from None
raise MyException() from None

 

Python异常-try、raise语句及自定义异常类

标签:erro   自定义异常   自定义   ISE   异常类   div   处理   on()   nbsp   

原文地址:https://www.cnblogs.com/hanxiaomeng/p/12801222.html


评论


亲,登录后才可以留言!