''' 常见异常 ''' from model.User import User ''' AttributeError异常 试图访问一个类中不存在的成员(包括:成员变量、属性和成员方法)而引发的异常 AttributeError: 'User' object has no attribute 'aaa' ''' user = User("chao", 18) print(user.aaa) ''' OSError异常 操作系统相关异常 FileNotFoundError:[Error 2] No such file or directory: 'abc.txt' ''' ''' IndexError异常 访问序列元素时,下标索引超出取值范围所引发的异常 IndexError: list index out of range ''' ''' KeyError异常 试图访问字典里不存在的键时而引发的异常 >>> dict[104] Traceback(most recent call last): File"<pyshell#14>", line1, in <module> dict1[104] KeyError: 104 ''' ''' NameError异常 试图使用一个不存在的变量而引发的异常 NameError: name 'value1' is not defined ''' ''' TypeError异常 传入变量类型与要求的不符合时而发生的异常 >>> i = '2' >>> print(5 / i) Traceback(most recent call last): File"<pyshell#20>", line1, in <module> print(5 / i) TypeError: unsupported operand type(s) for /: 'int' and 'str' ''' ''' ValueError异常 传入一个无效的参数值而引发的异常 >>> i = 'QWE' >>> print(5 / int(i)) Traceback(most recent call last): File"<pyshell#22>", line1, in <module> print(5 / int(i)) ValueError: invalid literal for int() with base 10: 'QWE' '''
评论
评论功能已经关闭!