设为首页 收藏本站
查看: 1263|回复: 0

[经验分享] Python基础教程之第8章 异常

[复制链接]

尚未签到

发表于 2017-5-5 12:14:18 | 显示全部楼层 |阅读模式
#Chapter 8 异常
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
#8.1 什么是异常
>>> 1/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
#8.2 按自己的方式出错
#8.2.1 raise语句
>>> raise Exception
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception
>>> raise Exception('hyperdrive overload')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception: hyperdrive overload
>>> import exceptions
>>> dir(exceptions)
['ArithmeticError', 'AssertionError', 'AttributeError', ...]
>>> raise ArithmeticError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ArithmeticError
#8.2.2 自定义异常类
>>> class SomeCustomException(Exception): pass
...
#8.3 捕捉异常
try:
x=input('Enter the first number: ')
y=input('Enter the second number: ')
print x/y
except ZeroDivisionError:
print "The second number can't be zero!"

#打开或关闭屏蔽
class MuffleCalculator:
muffled = False
def calc(self, expr):
try:
return eval(expr)
except ZeroDivisionError:
if self.muffled:
print 'Division by zero is illegal'
else:
raise
calculator=MuffleCalculator()
print calculator.calc('10/2')
#print calculator.calc('10/0') # No muffling
#5
#Traceback (most recent call last):
#  File "tta.py", line 14, in <module>
#    print calculator.calc('10/0') # No muffling
#  File "tta.py", line 5, in calc
#    return eval(expr)
#  File "<string>", line 1, in <module>
#ZeroDivisionError: integer division or modulo by zero
calculator.muffled=True
calculator.calc('10/0')
#Division by zero is illegal
#8.4 不止一个 except 语句
try:
x=input('Enter the first number: ')
y=input('Enter the second number: ')
print x/y
except ZeroDivisionError:
print "The second number can't be zero!"
except TypeError:
print "That wasn't a number, was it?"
#8.5 用一个块捕捉两个异常
try:
x=input('Enter the first number: ')
y=input('Enter the second number: ')
print x/y
except (ZeroDivisionError, TypeError, NameError):
print 'Your numbers are bogus...'
#8.6 捕捉对象
try:
x=input('Enter the first number: ')
y=input('Enter the second number: ')
print x/y
except (ZeroDivisionError, TypeError, NameError), e:
print e
#在Python 3.0 中, except子句会被写作 except (ZeroDivisionError, TypeError, NameError) as e
#8.7 真正的全捕捉
try:
x=input('Enter the first number: ')
y=input('Enter the second number: ')
print x/y
except:
print 'Something wrong happened...'

>>> x=input('Enter the first number: ')
Enter the first number: 10
>>> y=input('Enter the second number: ')
Enter the second number: 0
>>> print x/y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
#8.8 万事大吉
>>> try:
...     print 'A simple task'
... except:
...     print 'What? Something went wrong?'
... else:
...     print 'Ah... It went as planned.'
...
A simple task
Ah... It went as planned.
#只要有错误发生, 程序会不断要求重新输入
while True:
try:
x=input('Enter the first number: ')
y=input('Enter the second number: ')
value = x/y
print 'x/y is', value
except:
print 'Invalid input. Please try again.'
else:
break
#python ttb.py
#Enter the first number: 1
#Enter the second number: 0
#Invalid input. Please try again.
#Enter the first number: 'foo'
#Enter the second number: 'bar'
#Invalid input. Please try again.
#Enter the first number: baz
#Invalid input. Please try again.
#Enter the first number: 10
#Enter the second number: 2
#x/y is 5
#打印更加有用的错误信息:
while True:
try:
x=input('Enter the first number: ')
y=input('Enter the second number: ')
value = x/y
print 'x/y is', value
except Exception, e:
print 'Invalid input:', e
print 'Please try again'
else:
break
#python ttc.py
#Enter the first number: 1
#Enter the second number: 0
#Invalid input: integer division or modulo by zero
#Please try again
#Enter the first number: 'x'
#Enter the second number: 'y'
#Invalid input: unsupported operand type(s) for /: 'str' and 'str'
#Please try again
#Enter the first number: quuux
#Invalid input: name 'quuux' is not defined
#Please try again
#Enter the first number: 10
#Enter the second number: 2
#x/y is 5
#8.8 最后
>>> x=None
>>> try:
...     x = 1/0
... finally:
...     print 'Cleaning up...'
...     del x
...
Cleaning up...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: integer division or modulo by zero
>>>
>>> try:
...     1/0
... except NameError:
...     print 'Unknown variable'
... else:
...     print 'That went well!'
... finally:
...     print 'Cleaning up.'
...
Cleaning up.
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: integer division or modulo by zero
#异常和函数
>>> def faulty():
...     raise Exception('Someting is wrong')
...
>>> def ignore_exception():
...     faulty()
...
>>> def handle_exception():
...     try:
...             faulty()
...     except:
...             print 'Exception handled'
...
>>>
>>> ignore_exception()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in ignore_exception
File "<stdin>", line 2, in faulty
Exception: Something is wrong
>>>
>>> handle_exception()
Exception handled
>>>
#8.11 异常之禅
>>> def describePerson(person):
...     print 'Description of', person['name']
...     print 'Age:', person['age']
...     if 'occupation' in person:
...             print 'Occupation:', person['occupation']
...
def describePerson(person):
print 'Description of', person['name']
print 'Age:', person['age']
try:
print 'Occupation: ' + person['occupation']
except KeyError: pass
person = {'name':'Jonathan', 'age':33}
describePerson(person)
#python ttd.py
#Description of Jonathan
#Age: 33
person = {'name':'Jonathan', 'age':33, 'occupation':'IBIT Support'}
describePerson(person)
#python ttd.py
#Description of Jonathan
#Age: 33
#Occupation: IBIT Support
#8.12 小结
# 异常对象
# 引发异常
# 自定义异常类
# 捕捉异常: 使用try语句的except子句捕捉异常. 如果在except子句中不特别指定异常类, 那么所有的异常都会被捕捉.
# 异常可以放在 元组 中以实现多个异常的指定.
# 如果给except提供两个参数, 第二个参数就会绑定到 异常对象 上
# 在一个try语句中,可以包含多个except子句, 用来分别处理不同的异常
#else子句 如果主块try中没有发生异常, else子句就会被执行
#finally子句-- 如果需要确保某些代码不管是否有异常引发都要执行(比如清理代码, 关闭文件, 关闭网络套接字, 关闭数据库连接),
# 那么这些代码可以放置在finally子句中
#异常和函数 在函数内引发异常时, 它就会被传播到函数调用的地方(对于方法也是一样)
#8.12.1 新函数
# warnings.filterwarning(action....)用于过滤警告
#Exception所有异常的基类
#AttributeError特性引用或赋值失败时引发
#IOError试图打开不存在的文件(包括其他情况)时引发
#IndexError在使用序列中不存在的索引时引发
#KeyError在使用映射中不存在的键时引发
#NameError在找不到名字(变量)时引发
#SyntaxError在代码为错误形式时引发
#TypeError在内建操作或函数应用于错误类型的对象时引发
#ValueError在内建操作或函数应用于正确类型的对象,但该对象使用不合适的值时引发
#ZeroDivisionError在除法或模除操作的第二个参数为0时引发

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373468-1-1.html 上篇帖子: Python基础教程之第1章 基础知识 下篇帖子: python 核心编程学习笔记(第3章) 对应Let's-python视频第4集
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表