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

python 异常处理

时间:2018-10-09 14:23:22      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:cep   final   color   err   spl   sel   src   nal   span   

技术分享图片
 1 try                              
 2 
 3      code
 4 
 5 except (error1,error2)  as e:
 6 
 7     print(e)
 8 
 9 except Exception as e 抓住所有错误,不建议用
10 
11    print(未知错误’,e)
12 
13 else: 
14 
15   print(一切正常16 
17 Finally:
18 
19   print(不管有没有错,都执行‘)’
View Code

自定义异常

技术分享图片
 1 class ShaohuiException(Exception)
 2        def __init__(self,msg):
 3                self.message=msg
 4        #def __str__(self):
 5        #      return self.message
 6 
 7 
 8 try
 9       raise ShaohuiException(我的异常’)
10 except  ShaohuiException as e:
11       print(e)
View Code

 

  

python 异常处理

标签:cep   final   color   err   spl   sel   src   nal   span   

原文地址:https://www.cnblogs.com/liushaohui/p/9759969.html

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