# 什么是异常? # 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行。 # 一般情况下,在Python无法正常处理程序时就会发生一个异常。 # 异常是Python对象,表示一个错误。 # 当Python脚本发生异常时我们需要捕获处理它,否则程序会终止执行。 # 演示异常 # f = open("123.txt","r") # FileNotFoundError: [Errno 2] No such file or directory: '123.txt' : 没有这样的文件或目录:'123.txt' # list =[1,2,3] # print(list[4]) # IndexError: list index out of range : 报错:列表索引超出范围 # str="jack" # str.index("v") # ValueError: substring not found : 没有找到结果:没有找到子字符串 # c= 5/0 # print(c) # ZeroDivisionError: division by zero : 表现为除数为0的一种Python异常:除零 # cj = input("请输入你的成绩:") # if 60<=ji<85: # print("及格") # elif 85<=if<=100: # print("优秀") # else: # print("不及格") # SyntaxError: invalid syntax : SyntaxError:无效的语法 # cj = input("请输入你的成绩:") # if 60<=cj<85: # print("及格") # elif 85<=cj<=100: # print("优秀") # else: # print("不及格") # TypeError: '<=' not supported between instances of 'int' and 'str' # TypeError: '<='不支持在'int'和'str'之间的实例 # zd = {"name":'jack',"age":18} # print(zd["names"]) # KeyError: 'names' : 元素不存在:'names' # 异常处理 # 作用 # 捕捉异常可以使用try/except语句。 # try/except语句用来检测try语句块中的错误,从而让except语句捕获异常信息并处理。 # 如果你不想在异常发生时结束你的程序,只需在try里捕获它。 # 语法1 # try: # open("qwe.txt","r") # print("123") # except FileNotFoundError: # print("异常处理") # else: # print("没有异常") # 语法2 # try: # open("qwe.txt","r") # print("123") # except FileNotFoundError as result: # print("异常处理",result) # else: # print("没有异常") # 使用except而不带任何异常类型 # 语法 # try: # open("qwe.txt","r") # print("123") # except : # print("异常处理") # else: # print("没有异常") # 以上方式try-except语句捕获所有发生的异常。但这不是一个很好的方式,我们不能通过该程序识别出具体的异常信息。因为它捕获所有的异常。 # 使用except而带多种异常类型 # 语法 # list = [1,2,3,4] # try: # open("qwe.txt", "r") # list[7] # except (NameError,FileNotFoundError) as rese: # print("出现异常",rese) # else: # print("没有异常") # try-finally 语句 # try-finally 语句无论是否发生异常都将执行最后的代码 # 语法 # try: # fh = open("test.txt", "r") # fh.readlines() # fh.close() # finally: # print("Error: 没有找到文件或读取文件失败") # 当在try块中抛出一个异常,立即执行finally块代码。 # finally块中的所有语句执行后,异常被再次触发,并执行except块代码。 # 参数的内容不同于异常 # 异常的传递 # def chuandi(): # print("---chuandi---") # print(num) # print("---chuandi---") # def chuandi2(): # print("---chuandi2---") # chuandi() # print("---chuandi2---") # def chuandi3(): # try: # print("---chuandi3---") # chuandi2() # print("---chuandi3---") # except Exception as cuwu: # print(cuwu) # print('---chuandi3---') # chuandi3() # chuandi2() # 触发异常 # 可以使用raise语句自己触发异常 # 输入考生的成绩(0~100) # def kaoSheng(cj): # if cj < 0 or cj >100: # raise Exception("Invalid score!", cj) # # 触发异常后,后面的代码就不会再执行 # kaoSheng(200) # 用户自定义异常 # 通过创建一个新的异常类,程序可以命名它们自己的异常。异常应该是典型的继承自Exception类,通过直接或间接的方式 # class ShortInputException(Exception): # def __init__(self, length, atleast): # self.length = length # self.atleast = atleast # def main(): # try: # s = input('请输入 --> ') # if len(s) < 3: # #raise引发一个你定义的异常 # raise ShortInputException(len(s), 3) # except ShortInputException as result:#x这个变量被绑定到了错误的实例 # print('ShortInputException: 输入的长度是 %d,长度至少应是 %d'%(result.length, result.atleast)) # else: # print('没有异常发生') # main()
python异常
最新推荐文章于 2024-07-18 15:53:23 发布