|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
原文:
- Help on class BaseException in module builtins:
- class BaseException(object)
- | Common base class for all exceptions
- |
- | Built-in subclasses:
- | BaseExceptionGroup
- | Exception
- | GeneratorExit
- | KeyboardInterrupt
- | ... and 1 other subclasses
- |
- | Methods defined here:
- |
- | __getattribute__(self, name, /)
- | Return getattr(self, name).
- |
- | __init__(self, /, *args, **kwargs)
- | Initialize self. See help(type(self)) for accurate signature.
- |
- | __reduce__(self, /)
- | Helper for pickle.
- |
- | __repr__(self, /)
- | Return repr(self).
- |
- | __setstate__(self, object, /)
- |
- | __str__(self, /)
- | Return str(self).
- |
- | add_note(self, object, /)
- | Exception.add_note(note) --
- | add a note to the exception
- |
- | with_traceback(self, object, /)
- | Exception.with_traceback(tb) --
- | set self.__traceback__ to tb and return self.
- |
- | ----------------------------------------------------------------------
- | Static methods defined here:
- |
- | __new__(*args, **kwargs)
- | Create and return a new object. See help(type) for accurate signature.
- |
- | ----------------------------------------------------------------------
- | Data descriptors defined here:
- |
- | __cause__
- | exception cause
- |
- | __context__
- | exception context
- |
- | __dict__
- |
- | __suppress_context__
- |
- | __traceback__
- |
- | args
复制代码
翻译:
- 模块内置类中有关 BaseException 的帮助:
- 类 BaseException(object)
- | 所有异常的通用基类
- |
- | 内置子类:
- | BaseExceptionGroup
- | 异常
- | 生成器退出
- | 键盘中断
- | ... 和 1 个其他子类
- |
- | 这里定义的方法:
- |
- | __getattribute__(self, name, /)
- | 返回 getattr(self,name)。
- |
- | __init__(self, /, *args, **kwargs)
- | 初始化自我。 有关准确的签名,请参见 help(type(self))。
- |
- | __reduce__(self, /)
- | pickle 的助手。
- |
- | 返回 repr(self)。
- | 返回 repr(self)。
- |
- | __setstate__(self, object, /)
- |
- |__str__(self, /)
- | 返回 str(self)。
- |
- | add_note(self, object, /)
- | Exception.add_note(note) --
- | 为异常添加注释
- |
- | with_traceback(self, object, /)
- | Exception.with_traceback(tb) -- | 为异常添加注释。
- | 将 self.__traceback__ 设为 tb 并返回 self。
- |
- | ----------------------------------------------------------------------
- | 此处定义的静态方法:
- |
- | __new__(*args, **kwargs)
- | 创建并返回一个新对象。 有关准确的签名,请参见 help(type)。
- |
- | ----------------------------------------------------------------------
- | 此处定义的数据描述符:
- |
- | __cause__
- | 异常原因
- |
- | __context__
- | 异常上下文
- |
- | __dict__
- |
- | __suppress_context__
- |
- | __traceback__
- |
- | args
复制代码
BaseException是python所有内置异常类的父类.任何你自己定义的异常类,如果你希望它继承 Python 内置的异常处理行为,可以直接或间接地继承 BaseException 类.
举例:
- class WithdrawalError(BaseException):
- def __init__(self, balance, amount):
- self.balance = balance
- self.amount = amount
- def __str__(self):
- return (f'Error: Insufficient balance ({self.balance}) '
- f'to withdraw {self.amount}')
- def withdraw(balance, amount):
- if balance < amount:
- raise WithdrawalError(balance, amount)
- else:
- return balance - amount
- try:
- balance = 100
- withdraw_amount = 200
- new_balance = withdraw(balance, withdraw_amount)
- print('Withdrawal successful. New balance:', new_balance)
- except WithdrawalError as e:
- print(e)
复制代码
在这个例子中,我们定义了一个自定义异常类 WithdrawalError,它继承自 BaseException。当尝试从账户中提取金额超过余额时,我们会引发 WithdrawalError 异常.
在 withdraw 函数中,我们检查账户余额是否足够,如果不足,则抛出 WithdrawalError 异常。在try块中,我们尝试调用withdraw函数,并处理可能抛出的 WithdrawalError 异常。如果抛出了异常,我们捕获并打印异常消息,否则,我们打印提款成功的消息和新的余额。
#上面的例子是从网上找的,自己想不出啥来了
与Exception的区别:
Exception是BaseException的子类,作为子类的 Exception 无法截获父类 BaseException 类型的错误. |
|