Python 中的具体异常
Python 中有一些常见的异常。这些异常通常在不同的程序中引发。它们可能是由程序员显式引发,也可能是 Python 解释器隐式引发。其中一些异常包括:
异常 AssertionError
当断言语句失败时,可能会引发 AssertionError。在 Python 中,我们也可以在代码中设置一些断言语句。断言语句必须始终为真。如果条件失败,它将引发 AssertionError。
示例代码
class MyClass:
def __init__(self, x):
self.x = x
assert self.x > 50
myObj = MyClass(5)
输出
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-21-71785acdf821> in <module>()
4 assert self.x > 50
5
----> 6 myObj = MyClass(5)
<ipython-input-21-71785acdf821> in __init__(self, x)
2 def __init__(self, x):
3 self.x = x
----> 4 assert self.x > 50
5
6 myObj = MyClass(5)
AssertionError:
异常 AttributeError
当我们尝试访问类的某个属性,但该属性不存在时,可能会引发 AttributeError。
示例代码
class point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def getpoint(self):
print('x= ' + str(self.x))
print('y= ' + str(self.y))
print('z= ' + str(self.z))
pt = point(10, 20)
pt.getpoint()
输出
x= 10
y= 20
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-15-f64eb52b2192> in <module>()
10
11 pt = point(10, 20)
---> 12 pt.getpoint()
<ipython-input-15-f64eb52b2192> in getpoint(self)
7 print('x= ' + str(self.x))
8 print('y= ' + str(self.y))
----> 9 print('z= ' + str(self.z))
10
11 pt = point(10, 20)
AttributeError: 'point' object has no attribute 'z'
异常 ImportError
当**import**语句遇到导入某个模块的问题时,可能会发生 ImportError。当 from 语句的包名称正确,但找不到指定名称的模块时,也可能会引发此异常。
示例代码
from math import abcd def __init__(self, x=0, y=0):
输出
--------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-23-ff4519a07c77> in <module>() ----> 1 from math import abcd ImportError: cannot import name 'abcd'
异常 ModuleNotFoundError
它是 ImportError 的子类。当找不到模块时,可能会引发此错误。当 sys.modules 中找不到**None**时,也可能会引发此错误。
示例代码
import abcd
输出
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) <ipython-input-24-7b45aaa048eb> in <module>() ----> 1 import abcd ModuleNotFoundError: No module named 'abcd'
异常 IndexError
当序列(列表、元组、集合等)的下标超出范围时,可能会引发 IndexError。
示例代码
myList = [10, 20, 30, 40] print(str(myList[5]))
输出
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-29-a86bd85b04c9> in <module>()
1 myList = [10, 20, 30, 40]
----> 2 print(str(myList[5]))
IndexError: list index out of range
异常 RecursionError
RecursionError 是一个运行时错误。当超过最大递归深度时,它将被引发。
示例代码
def myFunction(): myFunction() myFunction()
输出
---------------------------------------------------------------------------
RecursionError Traceback (most recent call last)
<ipython-input-39-8a59e0fb982f> in <module>()
2 myFunction()
3
----> 4 myFunction()
<ipython-input-39-8a59e0fb982f> in myFunction()
1 def myFunction():
----> 2 myFunction()
3
4 myFunction()
... last 1 frames repeated, from the frame below ...
<ipython-input-39-8a59e0fb982f> in myFunction()
1 def myFunction():
----> 2 myFunction()
3
4 myFunction()
RecursionError: maximum recursion depth exceeded
异常 StopIteration
在 Python 中,我们可以通过名为 next() 的内置方法获得 StopIteration 错误。当迭代器没有更多元素时,next() 方法将引发 StopIteration 错误。
示例代码
myList = [10, 20, 30, 40] myIter = iter(myList) while True: print(next(myIter))
输出
10
20
30
40
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-42-e608e2162645> in <module>()
2 myIter = iter(myList)
3 while True:
----> 4 print(next(myIter))
StopIteration:
异常 IndentationError
当 Python 代码中存在无效缩进时,它将引发此类错误。它继承了 Python 的**SyntaxError**类。
示例代码
for i in range(10):
print("The value of i: " + str(i))
输出
File "<ipython-input-44-d436d50bbdc8>", line 2
print("The value of i: " + str(i))
^
IndentationError: expected an indented block
异常 TypeError
当对不适当类型的对象执行操作时,可能会发生 TypeError。例如,如果我们在数组索引中提供浮点数,它将返回 TypeError。
示例代码
muList = [10, 20, 30, 40] print(myList[1.25])
输出
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-46-e0a2a05cd4d4> in <module>()
1 muList = [10, 20, 30, 40]
----> 2 print(myList[1.25])
TypeError: list indices must be integers or slices, not float
异常 ZeroDivisionError
当除法运算的分母为 0(零)时,将引发 ZeroDivisionError。
示例代码
print(5/0)
输出
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) <ipython-input-48-fad870a50e27> in <module>() ----> 1 print(5/0) ZeroDivisionError: division by zero
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP