简述

Python 提供了丰富的标准异常,这些异常非常灵活,我们甚至可以根据需要进行修改以满足特定需求,所以应该尽可能地使用它们。

然而,在实际编程中,你会发现有时标准异常不够用,这就需要我们创建自定义异常来满足特殊的业务场景。

| 版权声明:一去、二三里,未经博主允许不得转载。

自定义异常

创建自定义异常,可以通过创建一个新类来实现,这个新类必须从 ​​Exception​​ 类直接或间接地派生出来。

注意: 大多数标准异常也是 ​​Exception​​​ 类的派生形式,可参见:内置异常的层次结构。

例如,创建一个派生自 ​​Exception​​ 的自定义异常:

>>> class CustomException(Exception):
... pass
...

与其他异常一样,可以使用 ​​raise​​ 语句来引发此异常:

>>> raise CustomException
Traceback (most recent call last):
...
__main__.CustomException
>>>
>>> raise CustomException("A custom exception occurred")
Traceback (most recent call last):
...

实现异常类,比较好的做法是:将所有自定义异常放在一个单独的文件中(例如:​​exceptions.py​​​ 或 ​​errors.py​​),许多标准模块也都是这样做的。

既然自定义异常是类,那么它必然可以实现一个普通类能做的所有事情。但一般而言,应该尽量保持简单、简洁。大多数实现都是声明一个自定义基类,并从这个基类派生出其他的(由程序引发的)异常类。这是 Python 中实现自定义异常的标准方法,但并不仅限于这种方式。

实际案例

基于此,再完善下 Python 异常处理 中关于薪资的问题。输入一个数字,直到正确地猜测到薪资为止。为了解决这个问题,提供一些猜测大于/小于的提示。

定义了一个名为 ​​CustomException​​​ 的基类,其他两个异常(​​ValueTooSmallException​​​ 和 ​​ValueTooLargeException​​​)都派生自该类,将它们放入 ​​exceptions.py​​ 模块中:

class CustomException(Exception):
"""Base class for other exceptions"""
pass

class ValueTooSmallException(CustomException):
"""Raised when the input value is too small"""
pass

class ValueTooLargeException(CustomException):
"""Raised when the input value is too large"""
pass

然后,编写一个猜薪资的主程序 - ​​guess_salary.py​​:

from exceptions import *

# 需要猜测的薪资
salary = 10000

while True:
try:
i_salary = int(input("Please enter the salary: "))
if i_salary < salary:
raise ValueTooSmallException
elif i_salary > salary:
raise ValueTooLargeException
except ValueTooSmallException:
print("Too small, try again!\n")
except ValueTooLargeException:
print("Too large, try again!\n")
except:
print("It's not a number!\n")
else:
print("Congratulations!")
break

考虑到程序的健壮性,除了自定义异常之外,我们还捕获了其他异常。这样以来,即使输入的内容无法转换为 int,程序也不至于出错。

运行主程序,并输入相关值:

Please enter the salary: abc
It’s not a number!

Please enter the salary: 5000
Too small, try again!

Please enter the salary: 20000
Too large, try again!

Please enter the salary: 10000
Congratulations!

完美,一切正如我们所愿!