Python中的具体异常
python中有一些常见的例外。这些异常通常在不同的程序中提出。这些可以由程序员显式地引发,或者python解释器可以隐式地引发这些类型的异常。其中一些例外是-
异常AssertionError
断言语句失败时,可能引发AssertionError。在python中,我们可以在代码中设置一些assert语句。assert语句必须始终为true。如果条件失败,它将引发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
当我们尝试访问类的某个属性,但其中不存在属性错误时,可能会引发属性错误。
范例程式码
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
当导入语句在导入某些模块时遇到问题时,可能会发生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
当序列的下标(列表,元组,集合等)超出范围时,可能会出现索引错误。
范例程式码
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中,我们可以通过名为的内置方法来获取StopIteration错误next()。当一个迭代器没有其他元素时,该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