在Python 3中已经存在异常时提升异常 [英] Raising exceptions when an exception is already present in Python 3

查看:156
本文介绍了在Python 3中已经存在异常时提升异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在第二个( B )在以下代码中引发时,第一个异常( A )会发生什么? ?

  class A(Exception):pass 
class B(Exception):pass

尝试:
try:
raise A('first')
finally:
raise B('second')
除了X作为c:
print(c)

如果以 X = A 我得到:

追溯(最近的最后一次呼叫):
文件raise_more_exceptions.py,第6行,
提高A('first')
__main __。A:first

在处理上述异常时发生另一个异常:

追溯(最近的最后一次调用) :
文件raise_more_exceptions.py,第8行
raise B('second')
__main __。B:second

但是如果 X = B 我得到:

第二个



问题




  1. 我的第一个例外

  2. 为什么只有最外层的异常可以捕获?

  3. 如何剥离最外面的异常并重新提及早期的异常?



Update0



这个问题专门针对Python 3,因为它的异常处理与Python 2。

解决方案

Pythons异常处理一次只处理一个例外。但是,异常对象受到与其他所有内容相同的可变规则和垃圾收集的约束。因此,如果将异常对象保存在某个变量的某个地方,则可以稍后处理该变量。即使引发了另一个异常。



在您的情况下,当引发异常时在finally语句中,Python 3将在第二个异常之一之前打印出第一个异常的追溯,以便更有帮助。



更常见的情况是要在显式异常处理期间引发异常。然后,您可以在下一个异常中保存异常。只需将其作为参数传递:

 >>> A类(异常):
...通过
...
>>> B类(例外):
...通过
...
>>>尝试:
...尝试:
... raise A('first')
...除了A作为e:
... raise B('second' ,e)
...除了例外c:
... print(c.args [1])$ ​​b $ b ...
first

如您所见,您现在可以访问原始例外。


What happens to my first exception (A) when the second (B) is raised in the following code?

class A(Exception): pass
class B(Exception): pass

try:
    try:
        raise A('first')
    finally:
        raise B('second')
except X as c:
    print(c)

If run with X = A I get:

Traceback (most recent call last):
  File "raising_more_exceptions.py", line 6, in 
    raise A('first')
__main__.A: first

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "raising_more_exceptions.py", line 8, in 
    raise B('second')
__main__.B: second

But if X = B I get:

second

Questions

  1. Where did my first exception go?
  2. Why is only the outermost exception catchable?
  3. How do I peel off the outermost exception and reraise the earlier exceptions?

Update0

This question specifically addresses Python 3, as its exception handling is quite different to Python 2.

解决方案

Pythons exception handling will only deal with one exception at a time. However, exception objects are subject to the same variable rules and garbage collection as everything else. Hence, if you save the exception object in a variable somewhere you can deal with it later, even if another exception is raised.

In your case, when an exception is raised during the "finally" statement, Python 3 will print out the traceback of the first exception before the one of the second exception, to be more helpful.

A more common case is that you want to raise an exception during an explicit exception handling. Then you can "save" the exception in the next exception. Just pass it in as a parameter:

>>> class A(Exception):
...     pass
... 
>>> class B(Exception):
...     pass
... 
>>> try:
...     try:
...         raise A('first')
...     except A as e:
...         raise B('second', e)
... except Exception as c:
...     print(c.args[1])
... 
first

As you see you can now access the original exception.

这篇关于在Python 3中已经存在异常时提升异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆