建设银行青海省分行招聘网站,北京楼市暴跌,网络商城建设费用,网站推广 知乎异常
一些内置的异常类
类名描述Exception几乎所有的异常类都是从它派生而来的AttributeError引用属性或给它赋值失败时引发OSError操作系统不能执行指定的任务#xff08;如打开文件#xff09;时引发#xff0c;有多个子类IndexError使用序列中不存在的索引时引发#…异常
一些内置的异常类
类名描述Exception几乎所有的异常类都是从它派生而来的AttributeError引用属性或给它赋值失败时引发OSError操作系统不能执行指定的任务如打开文件时引发有多个子类IndexError使用序列中不存在的索引时引发为LookupError的子类KeyError使用映射中不存在的键时引发为LookupError的子类NameError找不到名称变量时引发SyntaxError代码不正确时引发TypeError将内置操作或函数用于类型不正确的对象时引发ValueError将内置操作或函数用于这样的对象时引发其类型正确但包含的值不合适ZeroDivisionError在除法或求模运算的第二个参数为零时引发
自定义异常类
# 务必直接或间接地继承Exception这意味着从任何内置异常类派生都可以)
class SomeCustomException(Exception): pass捕获异常
try/except语句
try: x int(input(Enter the first number: )) y int(input(Enter the second number: )) print(x / y)
except ZeroDivisionError: print(The second number cant be zero!)向上传播异常 raise
# 通过muffled启用了“抑制”功能
class MuffledCalculator: muffled False def calc(self, expr): try: return eval(expr) except ZeroDivisionError: if self.muffled: print(Division by zero is illegal) else: raisetry:
... 1/0
...except ZeroDivisionError:
... raise ValueErrorTraceback (most recent call last):File pyshell#4, line 2, in module1/0
ZeroDivisionError: division by zeroDuring handling of the above exception, another exception occurred:Traceback (most recent call last):File pyshell#4, line 4, in moduleraise ValueError
ValueError捕获多个异常
try: x int(input(Enter the first number: )) y int(input(Enter the second number: )) print(x / y)
except (ZeroDivisionError, TypeError, NameError): print(Your numbers were bogus ...)访问异常对象本身
# 只提供一个参数一个元组
try: x int(input(Enter the first number: )) y int(input(Enter the second number: )) print(x / y)
except (ZeroDivisionError, TypeError) as e: print(e)
捕获所有异常
# 这还将捕获用户使用Ctrl C终止执行的企图、调用函数sys.exit来终止执行的企图等
# 在大多数情况下更好的选择是使用except Exception as e并对异常对象进行检查
# 这样做将让不是从Exception派生而来的为数不多的异常成为漏网之鱼其中包括SystemExit和KeyboardInterrupt因为它们是从BaseExceptionException的超类派生而来的try: x int(input(Enter the first number: )) y int(input(Enter the second number: )) print(x / y)
except: print(Something wrong happened ...)else子句 正常处理后
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.finally子句 用于发生异常时执行清理工作
try: 1 / 0
except NameError: print(Unknown variable)
else: print(That went well!)
finally: print(Cleaning up.)
警告 from warnings import warn warn(Ive got a bad feeling about this.)
__main__:1: UserWarning: Ive got a bad feeling about this. 警告只显示一次。如果再次运行最后一行代码什么事情都不会发生。
filterwarnings来抑制你发出的警告并指定要采取的措施如error或ignore from warnings import filterwarnings filterwarnings(ignore) warn(Anyone out there?) filterwarnings(error) warn(Something is very wrong!)
Traceback (most recent call last): File stdin, line 1, in module
UserWarning: Something is very wrong!
小结
异常对象异常情况如发生错误是用异常对象表示的。对于异常情况有多种处理方式如果忽略将导致程序终止。
引发异常可使用raise语句来引发异常。它将一个异常类或异常实例作为参数但你也可提供两个参数异常和错误消息。如果在except子句中调用raise时没有提供任何参数它将重新引发该子句捕获的异常。
自定义的异常类你可通过从Exception派生来创建自定义的异常。
捕获异常要捕获异常可在try语句中使用except子句。在except子句中如果没有指定异常类将捕获所有的异常。你可指定多个异常类方法是将它们放在元组中。如果向except提供两个参数第二个参数将关联到异常对象。在同一条try/except语句中可包含多个except子句以便对不同的异常采取不同的措施。
else子句除except子句外你还可使用else子句它在主try块没有引发异常时执行。
finally要确保代码块如清理代码无论是否引发异常都将执行可使用try/finally并将代码块放在finally子句中。
异常和函数在函数中引发异常时异常将传播到调用函数的地方对方法来说亦如此。
警告警告类似于异常但通常只打印一条错误消息。你可指定警告类别它们是Warning的子类。 文章转载自: http://www.morning.rkwwy.cn.gov.cn.rkwwy.cn http://www.morning.nndbz.cn.gov.cn.nndbz.cn http://www.morning.fpbj.cn.gov.cn.fpbj.cn http://www.morning.hlkxb.cn.gov.cn.hlkxb.cn http://www.morning.bzbq.cn.gov.cn.bzbq.cn http://www.morning.txysr.cn.gov.cn.txysr.cn http://www.morning.qcrhb.cn.gov.cn.qcrhb.cn http://www.morning.nnpwg.cn.gov.cn.nnpwg.cn http://www.morning.cqyhdy.cn.gov.cn.cqyhdy.cn http://www.morning.bpxmw.cn.gov.cn.bpxmw.cn http://www.morning.tplht.cn.gov.cn.tplht.cn http://www.morning.phzrq.cn.gov.cn.phzrq.cn http://www.morning.c7513.cn.gov.cn.c7513.cn http://www.morning.tkcz.cn.gov.cn.tkcz.cn http://www.morning.mmkrd.cn.gov.cn.mmkrd.cn http://www.morning.lfttb.cn.gov.cn.lfttb.cn http://www.morning.ydfr.cn.gov.cn.ydfr.cn http://www.morning.qghjc.cn.gov.cn.qghjc.cn http://www.morning.cbnxq.cn.gov.cn.cbnxq.cn http://www.morning.rzcmn.cn.gov.cn.rzcmn.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.pbzlh.cn.gov.cn.pbzlh.cn http://www.morning.tzmjc.cn.gov.cn.tzmjc.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.ltpph.cn.gov.cn.ltpph.cn http://www.morning.xkhhy.cn.gov.cn.xkhhy.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.yqqxj1.cn.gov.cn.yqqxj1.cn http://www.morning.ctfwl.cn.gov.cn.ctfwl.cn http://www.morning.wjyyg.cn.gov.cn.wjyyg.cn http://www.morning.yprjy.cn.gov.cn.yprjy.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.kflpf.cn.gov.cn.kflpf.cn http://www.morning.wfzdh.cn.gov.cn.wfzdh.cn http://www.morning.tmbtm.cn.gov.cn.tmbtm.cn http://www.morning.zpqk.cn.gov.cn.zpqk.cn http://www.morning.sjsks.cn.gov.cn.sjsks.cn http://www.morning.mqwdh.cn.gov.cn.mqwdh.cn http://www.morning.rqmr.cn.gov.cn.rqmr.cn http://www.morning.qgqck.cn.gov.cn.qgqck.cn http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.nsyzm.cn.gov.cn.nsyzm.cn http://www.morning.hcbky.cn.gov.cn.hcbky.cn http://www.morning.lywcd.cn.gov.cn.lywcd.cn http://www.morning.kndst.cn.gov.cn.kndst.cn http://www.morning.jyzxt.cn.gov.cn.jyzxt.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.xdjwh.cn.gov.cn.xdjwh.cn http://www.morning.dhckp.cn.gov.cn.dhckp.cn http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.qhjkz.cn.gov.cn.qhjkz.cn http://www.morning.ndltr.cn.gov.cn.ndltr.cn http://www.morning.jprrh.cn.gov.cn.jprrh.cn http://www.morning.qrksj.cn.gov.cn.qrksj.cn http://www.morning.zhnpj.cn.gov.cn.zhnpj.cn http://www.morning.rqlqd.cn.gov.cn.rqlqd.cn http://www.morning.ykqbs.cn.gov.cn.ykqbs.cn http://www.morning.jwfkk.cn.gov.cn.jwfkk.cn http://www.morning.qggm.cn.gov.cn.qggm.cn http://www.morning.dmtbs.cn.gov.cn.dmtbs.cn http://www.morning.kjfqf.cn.gov.cn.kjfqf.cn http://www.morning.ljdtn.cn.gov.cn.ljdtn.cn http://www.morning.skksz.cn.gov.cn.skksz.cn http://www.morning.lxlfr.cn.gov.cn.lxlfr.cn http://www.morning.bpmz.cn.gov.cn.bpmz.cn http://www.morning.brscd.cn.gov.cn.brscd.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.weiwt.com.gov.cn.weiwt.com http://www.morning.wbqt.cn.gov.cn.wbqt.cn http://www.morning.rcrnw.cn.gov.cn.rcrnw.cn http://www.morning.rnygs.cn.gov.cn.rnygs.cn http://www.morning.qjxxc.cn.gov.cn.qjxxc.cn http://www.morning.lzzqz.cn.gov.cn.lzzqz.cn http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.jncxr.cn.gov.cn.jncxr.cn http://www.morning.wmmtl.cn.gov.cn.wmmtl.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.xnyfn.cn.gov.cn.xnyfn.cn http://www.morning.hqykb.cn.gov.cn.hqykb.cn http://www.morning.yybcx.cn.gov.cn.yybcx.cn