常见异常 | 中文解释 |
IOError |
输入/输出异常;基本上是无法打开文件 |
ImportError | 无法引入模块或包;基本上是路径问题或名称错误 |
IndexError | 下标索引超出序列边界,比如当x只有三个元素,却试图访问x[5] |
KeyError | 试图访问字典里不存在的键 |
NameError | 使用一个还未被赋予对象的变量 |
IndentationError | 语法错误(的子类) ;代码没有正确对齐 |
SyntaxError | Python代码非法,代码不能编译 |
KeyboardInterrupt | Ctrl+C被按下 |
EOFError | Ctrl+D被按下 |
UnboundLocalError | 试图访问一个还未被设置的局部变量,基本上是由于另有一个同名的全局变量,导致你以为正在访问它 |
AttributeError | 试图访问一个对象没有的属性,比如myInst.foo,但是myInst没有属性foo |
ValueError | 传入一个调用者不期望的值,即使值的类型是正确的 |
TypeError | 传入对象类型与要求的不符合 |
1
2
3
4
|
>>> f = file(
'myfile.txt'
)
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
IOError: [Errno
2
] No such file or directory:
'myfile.txt'
|
1
2
3
4
|
>>>
import
xpleaf
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
ImportError: No module named xpleaf
|
1
2
3
4
5
6
7
|
>>> a = range(
3
)
>>> a
[
0
,
1
,
2
]
>>> a[
3
]
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
IndexError: list index out of range
|
1
2
3
4
5
|
>>> mydict={
'name'
:
'xpleaf'
}
>>> mydict[
'age'
]
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
KeyError:
'age'
|
1
2
3
4
|
>>> print xpleaf
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
NameError: name
'xpleaf'
is
not defined
|
1
2
3
4
5
6
7
|
>>>
for
i
in
range(
3
):
... print i
... print
'Error!'
File
"<stdin>"
, line
3
print
'Error!'
^
IndentationError: unexpected indent
|
1
2
3
4
5
6
7
8
9
10
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4/blog$ python Error.py
File
"Error.py"
, line
3
prin kd
^
SyntaxError: invalid syntax
>>>
for
File
"<stdin>"
, line
1
for
^
SyntaxError: invalid syntax
|
1
2
3
4
5
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4/blog$ python test.py
name:^CTraceback (most recent call last):
File
"test.py"
, line
2
,
in
<module>
name = raw_input(
'name:'
)
KeyboardInterrupt
|
1
2
3
4
5
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4/blog$ python EOF.py
name:Traceback (most recent call last):
File
"EOF.py"
, line
2
,
in
<module>
name = raw_input(
'name:'
)
EOFError
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
程序代码如下:
name =
'xpleaf'
def sayYourName(mark):
if
mark ==
1
:
name =
'yonghaoye'
else
:
print
'My name is:'
,name
sayYourName(
0
)
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4/blog$ python test.py
My name
is
:
Traceback (most recent call last):
File
"test.py"
, line
10
,
in
<module>
sayYourName(
0
)
File
"test.py"
, line
8
,
in
sayYourName
print
'My name is:'
,name
UnboundLocalError: local
var
iable
'name'
referenced before assignment
注意:如果是sayYourName(
1
),则不会出现问题,此时相当于在函数中定义了一个局部变量
|
1
2
3
4
5
6
7
8
9
10
11
|
>>>
class
myClass():
... pass
...
>>> myInst = myClass()
>>> myInst.bar =
'spam'
>>> myInst.bar
'spam'
>>> myInst.foo
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
AttributeError: myClass instance has no attribute
'foo'
|
1
2
3
4
|
>>> f = file(
'myfile.txt'
,
'io'
)
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
ValueError: mode string must begin
with
one of
'r'
,
'w'
,
'a'
or
'U'
, not
'io'
|
1
2
3
4
5
|
>>> num =
3
>>> str =
'name'
+ num +
'age'
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
TypeError: cannot concatenate
'str'
and
'int'
objects
|
1
2
3
4
5
6
7
8
9
10
11
|
代码如下:
try
:
a = [
1
,
2
,
3
]
a[
3
]
except IndexError:
print
'\033[32;1mIndexError!\033[0m'
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4/blog$ python test.py
IndexError!
xpleaf@xpleaf-
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
代码如下:
try
:
a = [
1
,
2
,
3
]
a[
3
]
mydict = {
'name'
:
'xpleaf'
}
mydict[
'age'
]
except KeyError:
print
'\033[32;1mKeyError!\033[0m'
except IndexError:
print
'\033[32;1mIndexError!\033[0m'
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4/blog$ python test.py
IndexError!
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
代码如下:
try
:
a = [
1
,
2
,
3
]
print a[
2
]
mydict = {
'name'
:
'xpleaf'
}
print mydict[
'name'
]
print
'Name:'
,name #显然会触发NameError
except KeyError:
print
'\033[32;1mKeyError!\033[0m'
except IndexError:
print
'\033[32;1mIndexError!\033[0m'
except:
print
'Something is wrong!'
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4/blog$ python test.py
3
xpleaf
Name: Something
is
wrong!
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
程序代码如下:
try
:
name = [
'a'
,
'b'
,
'c'
]
name[
2
]
mydict = {}
except IndexError,err:
print
'\033[32;1mIndexError!\033[0m'
,err
except KeyError:
print
'\033[32;1mKeyError!\033[0m'
except:
print
'\033[32;1mSomething is wrong!\033[0m'
else
:
print
'No Error!'
finally
:
print
'Going to exit...'
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4/blog$ python test.py
No Error!
Going to exit...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
代码如下:
class
XpleafException(Exception): #这里Exception是关键字
pass
try
:
name = raw_input(
'name:'
).strip()
if
name !=
'xpleaf'
:
raise XpleafException
except XpleafException:
print
'No valid name sepecfied...'
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4/blog$ python test.py
name:xpleaf
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4/blog$ python test.py
name:yonghaoye
No valid name sepecfied...
|