1.1异常问题举例
例一
>>> i = input('请输入数字')
请输入数字:0
>>> print(i)
0
>>> print(5 / int(i))
Traceback (most recent call last):
File "", line 1, in
ZeroDivisionError: division by zero
上述代码的报错是除零的错误,数学上不允许除零
例二
>>> i="QWE"
>>> print(5 / int(i))
Traceback (most recent call last):
File "", line 1, in
ValueError: invalid literal for int() with base 10: 'QWE'
i是字符串类型,而且是QWE
,不能转换成int类型,所以抛出异常
1.2异常类继承层次
Python中的异常根类是BaseException,结构图如下
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StandardError
| +-- BufferError
| +-- ArithmeticError
| | +-- FloatingPointError
| | +-- OverflowError
| | +-- ZeroDivisionError
| +-- AssertionError
| +-- AttributeError
| +-- EnvironmentError
| | +-- IOError
| | +-- OSError
| | +-- WindowsError (Windows)
| | +-- VMSError (VMS)
| +-- EOFError
| +-- ImportError
| +-- LookupError
| | +-- IndexError
| | +-- KeyError
| +-- MemoryError
| +-- NameError
| | +-- UnboundLocalError
| +-- ReferenceError
| +-- RuntimeError
| | +-- NotImplementedError
| +-- SyntaxError
| | +-- IndentationError
| | +-- TabError
| +-- SystemError
| +-- TypeError
| +-- ValueError
| +-- UnicodeError
| +-- UnicodeDecodeError
| +-- UnicodeEncodeError
| +-- UnicodeTranslateError
+-- Warning
+-- DeprecationWarning
+-- PendingDeprecationWarning
+-- RuntimeWarning
+-- SyntaxWarning
+-- UserWarning
+-- FutureWarning
+-- ImportWarning
+-- UnicodeWarning
+-- BytesWarning
1.3常见异常
1.3.1 AttributeError 异常
访问不存在的成员
例如
>>> class Animal(object):
pass
>>> a1 = Animal()
>>> a1.run()
Traceback (most recent call last):
File "", line 1, in
a1.run()
AttributeError: 'Animal' object has no attribute 'run'
>>>
>>> print(a1.age)
Traceback (most recent call last):
File "", line 1, in
print(a1.age)
AttributeError: 'Animal' object has no attribute 'age'
程序中创建了一个类叫Animal,但是并没有run函数和age变量,所以抛出异常
1.3.2 OSError 异常
OSError是操作系统相关异常,例如试图打开一个不存在的文件
>>> f=open("abc.txt")
Traceback (most recent call last):
File "", line 1, in
f=open("abc.txt")
FileNotFoundError: [Errno 2] No such file or directory: 'abc.txt'
1.3.3 IndexError 异常
是访问元素时,下标超出索引最大值或最小值引发异常例如:
>>> code_list = [125, 56, 89, 36]
>>> code_list[4]
Traceback (most recent call last):
File "", line 1, in
code_list[4]
IndexError: list index out of range
1.3.4 KeyError 异常
是试图访问字典里不存在的键引发,例如:
>>> dict1[104]
Traceback (most recent call last):
File "", line 1, in
dict1[104]
NameError: name 'dict1' is not defined
104在字典中不存在,O(∩_∩)O哈哈~那就报错吧
1.3.5 NameError 异常
NameError是指试图使用一个不存在的变量引发的异常,PythonShell中运行实例
>>> value1
Traceback (most recent call last):
File "", line 1, in
NameError: name 'value1' is not defined
>>> a = value1
Traceback (most recent call last):
File "", line 1, in
NameError: name 'value1' is not defined
>>> value1 = 10
>>> value1
10
赋值和直接取值都不行,因为没有这个value1
变量
只有最后的value1 = 10
,才让value1有了值
1.3.6 TypeError 异常
TypeError是试图传入变量类型与要求的不符合时而引发的异常。PythonShell运行实例、
>>> i = '2'
>>> print(5 / i)
Traceback (most recent call last):
File "", line 1, in
print(5 / i)
TypeError: unsupported operand type(s) for /: 'int' and 'str'
i是一个字符类型,而5是整形,不能进行除法运算,因为类型不统一
1.3.7 ValueError 异常
ValueError是由于传入一个无效的参数值而引发的异常,这个异常在前面已经遇到了
>>> del i
>>> i = 'QWE'
>>> print( 5 / int(i))
Traceback (most recent call last):
File "", line 1, in
print( 5 / int(i))
ValueError: invalid literal for int() with base 10: 'QWE'
1.4对于捕获异常的说明
我不会捕获异常,不会使用try
和except
等等,看也看不懂啊……