python 异常处理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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('不管有没有错,都执行‘)’
自定义异常
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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)
本章博客由作者刘少辉著写