Python基础--异常
2016-03-19 22:11
477 查看
Google C++ Style中禁止使用异常。
Python中也有异常,就简单分享一下。
1/0就会产生异常。
按自己的方式出错
raise语句
自定义异常类
捕捉异常 try/except
多个except
一个excep捕捉多个异常
try/except/else
finally语句
不管是否引发了异常,finall语句都会执行。
Python中也有异常,就简单分享一下。
1/0就会产生异常。
按自己的方式出错
raise语句
>>>raise Exception Traceback (most recent all last):
自定义异常类
class SomeCustomException(Exception):pass
捕捉异常 try/except
try: x = input('Enter the first number: ') y = input('Enter the second number: ') print x/y except ZeroDivisonError: print 'The second number can't be zero!'
多个except
try: x = input('Enter the first number: ') y = input('Enter the second number: ') print x/y except ZeroDivisonError: print 'The second number can't be zero!'
except TypeError:
print 'That wasn't a number, was it?'
一个excep捕捉多个异常
try: x = input('Enter the first number: ') y = input('Enter the second number: ') print x/y except (ZeroDivisonError, TypeError, NameError): print 'Your numbers were bogus...'
try/except/else
try: print' ' except: print' ' else: print' '
finally语句
不管是否引发了异常,finall语句都会执行。
相关文章推荐
- Python基础--异常
- 抓取搜狗壁纸公园的图片(四)改用python3.5下载真正的壁纸
- Python Challenge 9
- 用Python+Django在Eclipse环境下开发web网站
- python-2
- Python Static method vs class method
- Python代码
- python xpath 基本用法
- python和Java的区别
- 七步教你精通Python机器学习
- Python实现逻辑回归
- 据廖雪峰python3教程----python学习第三天
- python提取包含关键字的整行数据
- python orm框架-----SQLALchemy-查询篇
- Python –同步IO学习笔记
- python学习
- 压缩感知重构算法之IHT算法python实现
- python decorator装饰器
- [python]Graphlab的基本用法
- Python时间戳与时间字符串互相转换实例代码