Python 自定义异常

作者: 一去、二三里
个人微信号: iwaleon
微信公众号: 高效程序员

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):
...
__main__.CustomException: A custom exception occurred

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

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

实际案例

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

定义了一个名为 CustomException 的基类,其他两个异常(ValueTooSmallExceptionValueTooLargeException)都派生自该类,将它们放入 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!

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

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一去丶二三里

有收获,再打赏!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值