码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-04-29 12:45:48      阅读:94      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!