在Python中手动引发(引发)异常

2024-04-27 00:03:16 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在Python中引发异常,以便稍后通过except块捕获它?


Tags: except
3条回答

How do I manually throw/raise an exception in Python?

Use the most specific Exception constructor that semantically fits your issue

在您的信息中具体说明,例如:

raise ValueError('A very specific bad thing happened.')

不引发一般异常

避免引发一般异常。要捕获它,您必须捕获它的子类的所有其他更具体的异常。

问题1:隐藏bug

raise Exception('I know Python!') # Don't! If you catch, likely to hide bugs.

例如:

def demo_bad_catch():
    try:
        raise ValueError('Represents a hidden bug, do not catch this')
        raise Exception('This is the exception you expect to handle')
    except Exception as error:
        print('Caught this error: ' + repr(error))

>>> demo_bad_catch()
Caught this error: ValueError('Represents a hidden bug, do not catch this',)

问题2:抓不到

更具体的捕获不会捕获一般异常:

def demo_no_catch():
    try:
        raise Exception('general exceptions not caught by specific handling')
    except ValueError as e:
        print('we will not catch exception: Exception')


>>> demo_no_catch()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling

最佳实践:raise语句

Instead, use the most specific Exception constructor that semantically fits your issue

raise ValueError('A very specific bad thing happened')

它还方便地允许将任意数量的参数传递给构造函数:

raise ValueError('A very specific bad thing happened', 'foo', 'bar', 'baz') 

这些参数由异常对象的args属性访问。例如:

try:
    some_code_that_may_raise_our_value_error()
except ValueError as err:
    print(err.args)

印刷品

('message', 'foo', 'bar', 'baz')    

在Python 2.5中,一个实际的message属性被添加到BaseException中,以鼓励用户对异常进行子类划分,并停止使用args,但是the introduction of ^{} and the original deprecation of args has been retracted

最佳实践:except子句

例如,在except子句中,您可能希望记录发生了特定类型的错误,然后重新引发。在保留堆栈跟踪的同时,最好的方法是使用一个raise语句。例如:

logger = logging.getLogger(__name__)

try:
    do_something_in_app_that_breaks_easily()
except AppError as error:
    logger.error(error)
    raise                 # just this!
    # raise AppError      # Don't do this, you'll lose the stack trace!

不要修改你的错误。。。但如果你坚持的话

您可以使用sys.exc_info()来保留stacktrace(和错误值),但这是更容易出错的方式,而且在Python 2和3之间存在兼容性问题,更愿意使用裸raise来重新提升。

为了解释,sys.exc_info()返回类型、值和回溯。

type, value, traceback = sys.exc_info()

这是Python2中的语法-注意这与Python3不兼容:

    raise AppError, error, sys.exc_info()[2] # avoid this.
    # Equivalently, as error *is* the second object:
    raise sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]

如果您愿意,可以修改新加薪的内容,例如为实例设置新参数:

def error():
    raise ValueError('oops!')

def catch_error_modify_message():
    try:
        error()
    except ValueError:
        error_type, error_instance, traceback = sys.exc_info()
        error_instance.args = (error_instance.args[0] + ' <modification>',)
        raise error_type, error_instance, traceback

我们在修改args的同时保留了整个回溯。请注意,这不是最佳实践,而且在Python 3中是无效语法(使保持兼容性变得更加困难)。

>>> catch_error_modify_message()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in catch_error_modify_message
  File "<stdin>", line 2, in error
ValueError: oops! <modification>

Python 3中:

    raise error.with_traceback(sys.exc_info()[2])

再次:避免手动操作回溯。它是less efficient而且更容易出错。如果你使用线程和sys.exc_info,你甚至可能得到错误的回溯(特别是如果你对控制流使用异常处理,我个人倾向于避免这种情况)

Python 3,异常链接

在Python3中,可以链接异常,以保留跟踪:

    raise RuntimeError('specific message') from error

注意:

  • 允许更改引发的错误类型,并且
  • 这与Python 2不兼容。

不推荐的方法:

这些代码很容易隐藏,甚至可以进入生产代码。您想要引发异常,执行这些操作将引发异常,但不是预期的异常!

Valid in Python 2, but not in Python 3如下:

raise ValueError, 'message' # Don't do this, it's deprecated!

只有valid in much older versions of Python(2.4及更低版本),你可能仍然会看到人们在挑拨:

raise 'message' # really really wrong. don't do this.

在所有现代版本中,这实际上会引发TypeError,因为您没有引发BaseException类型。如果您没有检查正确的异常,并且没有知道该问题的审阅者,那么它可能会投入生产。

示例用法

如果用户不正确使用我的API,我会提出异常警告:

def api_func(foo):
    '''foo should be either 'baz' or 'bar'. returns something very useful.'''
    if foo not in _ALLOWED_ARGS:
        raise ValueError('{foo} wrong, use "baz" or "bar"'.format(foo=repr(foo)))

创建自己的错误类型

"I want to make an error on purpose, so that it would go into the except"

您可以创建自己的错误类型,如果您想指出应用程序中的某些特定错误,只需在异常层次结构中为适当的点创建子类即可:

class MyAppLookupError(LookupError):
    '''raise this when there's a lookup error for my app'''

使用方法:

if important_key not in resource_dict and not ok_to_be_missing:
    raise MyAppLookupError('resource is missing, and that is not ok.')

在Python3中,有4种不同的语法用于rasing异常:

1. raise exception 
2. raise exception (args) 
3. raise
4. raise exception (args) from original_exception

1. raise exception vs. 2. raise exception (args)

如果使用raise exception (args)引发异常,则打印异常对象时将打印args,如下例所示。

  #raise exception (args)
    try:
        raise ValueError("I have raised an Exception")
    except ValueError as exp:
        print ("Error", exp)     # Output -> Error I have raised an Exception 



  #raise execption 
    try:
        raise ValueError
    except ValueError as exp:
        print ("Error", exp)     # Output -> Error 

3.raise

没有任何参数的raise语句重新引发最后一个异常。 如果在捕获异常后需要执行某些操作,然后希望重新引发异常,则这非常有用。但是如果之前没有异常,raise语句会引发TypeError异常。

def somefunction():
    print("some cleaning")

a=10
b=0 
result=None

try:
    result=a/b
    print(result)

except Exception:            #Output ->
    somefunction()           #some cleaning
    raise                    #Traceback (most recent call last):
                             #File "python", line 8, in <module>
                             #ZeroDivisionError: division by zero

4. raise exception (args) from original_exception

此语句用于创建异常链,其中响应另一个异常而引发的异常可以包含原始异常的详细信息,如下例所示。

class MyCustomException(Exception):
pass

a=10
b=0 
reuslt=None
try:
    try:
        result=a/b

    except ZeroDivisionError as exp:
        print("ZeroDivisionError -- ",exp)
        raise MyCustomException("Zero Division ") from exp

except MyCustomException as exp:
        print("MyException",exp)
        print(exp.__cause__)

输出:

ZeroDivisionError --  division by zero
MyException Zero Division 
division by zero

DON'T DO THIS. Raising a bare Exception is absolutely not the right thing to do; see Aaron Hall's excellent answer instead.

再也没有比这更像Python了:

raise Exception("I know python!")

如果您想了解更多信息,请参见python的the raise statement docs

相关问题 更多 >