Python 自定义异常

网友投稿 308 2022-08-23


Python 自定义异常

简述

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

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

自定义异常

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

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

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

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

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

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

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

实际案例

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

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

class CustomException(Exception): """Base class for other exceptions""" passclass ValueTooSmallException(CustomException): """Raised when the input value is too small""" passclass ValueTooLargeException(CustomException): """Raised when the input value is too large""" pass

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

from exceptions import *# 需要猜测的薪资salary = 10000while 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!

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python 错误和异常(python是什么意思)
下一篇:带大家认识Java语法之泛型与通配符
相关文章

 发表评论

暂时没有评论,来抢沙发吧~