Python Errors and Exceptions
来源:互联网 发布:淘宝上怎么买客户资料 编辑:程序博客网 时间:2024/05/18 00:34
http://www.cnblogs.com/xuqiang/archive/2011/04/30/2033579.html
1. python中的try{}catch{}
2. raise exception
3. try...except ... else..
4. finally块
1. python中的try{}catch{}
python中的异常处理的关键字和c#中的是不相同的,python中使用try,except关键在来处理异常,如下:
def dive(x, y):
try:
result = x / y;
except ZeroDivisionError as z :
print("division by zero.", z);
else:
print("the result is ", result);
finally:
print("executing finally caluse.");
try:
result = x / y;
except ZeroDivisionError as z :
print("division by zero.", z);
else:
print("the result is ", result);
finally:
print("executing finally caluse.");
2. raise excepption
python中如果在except中如果需要将异常重新抛出可以使用关键字raise,类似于c#中的throw关键字。
def raise_test():
try:
result = 1 / 0;
except ZeroDivisionError :
print ("can not process the exception, to throw ...");
raise;
finally:
print("in finally block");
raise_test();
try:
result = 1 / 0;
except ZeroDivisionError :
print ("can not process the exception, to throw ...");
raise;
finally:
print("in finally block");
raise_test();
3. try...except ... else..
It is useful for code that must be executed if the try clause does not raise an exception
else块是说如果异常没有被抛出的情况下将被执行。
4. finally块
A finally clauseis always executed before leaving thetrystatement
finally块是一定会被执行的,不论异常是否产生。
作者:许强1. 本博客中的文章均是个人在学习和项目开发中总结。其中难免存在不足之处 ,欢迎留言指正。2. 本文版权归作者和博客园共有,转载时,请保留本文链接。
0 0
- Python Errors and Exceptions
- Python Errors and Exceptions
- Errors and Exceptions
- python tutorial 学习笔记(七)Errors and Exceptions
- The Python Tutorial 8——Errors and Exceptions
- Python Tutorial 学习笔记6 --Errors and Exceptions
- [译]The Python Tutorial#8. Errors and Exceptions
- Python--Errors,User-defined Exceptions
- Files and Exceptions: Dealing with Errors
- [python3教程]第八章.ERRORS AND EXCEPTIONS
- The Python Tutorial 3.2 - 8Errors and Exceptions(错误和异常)
- python错误Errors与异常Exceptions
- Python[十六]:Class and exceptions
- Use exceptions to watch for and handle I/O errors.
- Use exceptions to watch for and handle I/O errors.
- flask + MongoDB 学习笔记: Exceptions, Errors and Solutions
- Item 58: Used checked exceptions for recoverable conditions and runtime exceptions for programming errors
- Python - syntax errors and exception
- MFC Picture Control 控件不随图片伸缩的方法
- 程序员面试、算法研究、编程艺术、红黑树、数据挖掘5大系列集锦
- 限制鼠标活动
- 浙江新2014挂历制作,供应温州挂历印刷公司
- 如何通过dom4j构造出xml格式文件
- Python Errors and Exceptions
- 怎样书写项目方案
- 用最简单最实用的方式实现ViewPager无限循环两种方式
- int与Integer的区别
- sql语句中获取datetime的日期部分或时间部分
- JSP九大内置对象
- Java EE应用分层
- BB10方便查看qDebug()信息的方法
- ArcInfo实现数据同步复制【三】双向同步复制