魔兽6.0什么时候开,王近山简历,洱吧下载
当检测到一个错误时,解释器就无法继续进行了,反而出现一些错误的提示,这就是所谓的异常。
语法:
try: 可能发生的错误代码 except: 如果出现异常执行的代码
# 捕获指定异常: 这里是‘nameerror’ try: print(a) except nameerror as result: print(result) # name 'a' is not defined # 捕获所有异常 :exception try: print(b) except exception as result: print(result) # name 'c' is not defined # 如果尝试执行的代码的异常类型和要捕获的异常类型不一致,则无法捕获异常。 try: print(b) except ioerror as result: print(result) # 报错
try: print(1) # 1 except exception as result : print(result) else: print('没有异常时执行的代码') # 没有异常时执行的代码 finally: print('无论是否异常都要执行的代码') # 无论是否异常都要执行的代码 try: print(i) except exception as result : print(result) # name 'i' is not defined else: print('没有异常时执行的代码') finally: print('无论是否异常都要执行的代码') # 无论是否异常都要执行的代码
raise 抛出异常
class shortinputerror(exception): def __init__(self,length,min_len): self.length = length self.min_len = min_len def __str__(self): return f'你输入的长度是{self.length},不能少于{self.min_len}' try: con = input('input:') if len(con)<3: raise shortinputerror(len(con),3) except exception as res: print(res)
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python爬虫:Request Payload和Form Data的简单区别说明
浅谈Python中threading join和setDaemon用法及区别说明
Python3-异步进程回调函数(callback())介绍
python继承threading.Thread实现有返回值的子类实例
Python中使用threading.Event协调线程的运行详解
网友评论