没有争论 [英] raise with no argument

查看:177
本文介绍了没有争论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关加注声明的文档没有论据说

The documentation for the raise statement with no arguments says


如果没有表达式,则raise将重新提升当前作用域中活动的最后一个异常。

If no expressions are present, raise re-raises the last exception that was active in the current scope.

我以前认为这意味着当前函数必须执行,除了子句。阅读这个问题,并尝试一点,我认为这意味着堆栈上的任何函数必须执行,除了子句,但我不确定。另外,我已经意识到我不知道栈跟踪如何使用无参数加注:

I used to think that meant that the current function had to be executing an except clause. After reading this question and experimenting a little, I think it means that any function on the stack has to be executing an except clause, but I'm not sure. Also, I've realized I have no idea how the stack trace works with a no-arg raise:

def f():
  try:
    raise Exception
  except:
    g()

def g():
  raise

f()

生成

Traceback (most recent call last):
  File "foo", line 10, in <module>
    f()
  File "foo", line 5, in f
    g()
  File "foo", line 3, in f
    raise Exception
Exception

在初始加注时,看起来不像堆栈,或堆栈重新提升的时间,或者两个堆叠的连接,或任何我可以理解的东西。

That doesn't look like the stack at the time of the initial raise, or the stack at the time of the re-raise, or the concatenation of both stacks, or anything I can make sense of.

我是对无任何加注寻找任何函数在堆栈执行子句之外?另外,堆栈跟踪是如何工作的?

Am I right about a no-arg raise looking for any function on the stack executing an except clause? Also, how does the stack trace work on a reraise?

推荐答案

当你 raise 没有参数,解释器查找最后一个 异常并处理 。然后它的行为与使用 raise 一样,具有最新的异常类型,值和追溯。

When you raise without arguments, the interpreter looks for the last exception raised and handled. It then acts the same as if you used raise with the most recent exception type, value and traceback.

存储在当前线程的解释器状态中,并且可以使用 sys.exc_info() 。 处理我的意思是一个except子句捕获了异常。引用 尝试语句文档

This is stored in the interpreter state for the current thread, and the same information can be retrieved using sys.exc_info(). By 'handled' I mean that an except clause caught the exception. Quoting the try statement documentation:


在执行except子句的套件之前,有关异常的详细信息将分配给 sys module: sys.exc_type 接收标识异常的对象; sys.exc_value 接收异常的参数; sys.exc_traceback 接收一个追溯对象(参见标准类型层次结构标识程序中发生异常的点,这些细节也可以通过 sys.exc_info()函数获取,它返回一个元组(exc_type,exc_value,exc_traceback)

Before an except clause’s suite is executed, details about the exception are assigned to three variables in the sys module: sys.exc_type receives the object identifying the exception; sys.exc_value receives the exception’s parameter; sys.exc_traceback receives a traceback object (see section The standard type hierarchy identifying the point in the program where the exception occurred. These details are also available through the sys.exc_info() function, which returns a tuple (exc_type, exc_value, exc_traceback).

Python评估循环(C代码)中的实现说明 ,特别是:

See the implemenation notes in the Python evaluation loop (C code), specifically:


第二个
项目符号是向后兼容性的:它是(和)是共同的
有一个函数,当异常被捕获时被调用,并且
使该函数通过sys.exc_ZZZ访问捕获的异常。
(例如:traceback.print_exc())。

The second bullet was for backwards compatibility: it was (and is) common to have a function that is called when an exception is caught, and to have that function access the caught exception via sys.exc_ZZZ. (Example: traceback.print_exc()).

追溯返回cts你如何准确地重新提出。它是当前堆栈(第10行调用 f(),第5行调用 g())加上原始异常提出的位置:第3行。

The traceback reflects how you came to the re-raise accurately. It is the current stack (line 10 calling f(), line 5 calling g()) plus the original location of the exception raised: line 3.

这篇关于没有争论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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