从调用者的范围引发异常? [英] Raise an exception from the caller's scope?

查看:94
本文介绍了从调用者的范围引发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下脚本:

  def do_not_call_on_one(i):
如果i == 1:
提高ValueError(您永不听。)

打印成功!

do_not_call_on_one(1)

执行时,您将看到以下回溯:

 回溯(最近一次通话是最后一次):
文件 test.py,第7行,在<中。 >
do_not_call_on_one(1)
do_not_call_on_one中第3行的文件 test.py
引发ValueError(您从不听。)
ValueError:您从不听。

是否有某种方法可以操纵调用堆栈,从而从该行发出错误。实际引起问题的原因是这样的:

 跟踪(最近一次通话最近):
文件 test.py ,在< module>中的第7行
do_not_call_on_one(1)
ValueError:您从不听。

这将节省开发人员时间,否则将浪费时间扫描调用堆栈,查找使用错误的函数,可以提前定义正确的行为。



Python中是否有任何允许异常使用修改后的追溯的东西?



更新



有buitins正在复制此功能:

 #在test.py中:
int('a')

#执行'python test.py'的结果:
回溯(最近一次调用是最近一次):
文件 test.py ,<模块>中的第1行
int('a')
ValueError:以10为底的int()的无效文字:'a'

注意:跟踪不会下降到 int()函数中,以显示一堆无用的作用域(尤其是

解决方案

tl; dr:可能是:是的,但是我无法想象这是个好主意。



我要假设,如果我们足够努力,我们可以伪造一个调用栈来显示来自其他地方的异常。但是我认为这不是一个好主意。



首先,人们通常都理解引发异常的功能并不总是错误的。如果有人违反了合同,并且给了您一个意料之外的参数,可以提出一个例外。如果这是为了通过在调用方范围内引发异常来掩盖您的屁股,从而没人指责您的功能,那么我认为有人(也许您,也许是您的自动化测试的指责系统)需要重新考虑他们如何确定责任。 / p>

第二,我认为您不能很好地定义正确的范围。您可能会想,应该始终在呼叫者的范围内提出该建议,因为显然这不是您的错误。好吧,如果不是他们的错误呢?发生异常时,是否每个功能都应该举手说不是我的错吗?



即使您是正确的,并且您的功能是无可指摘的,您也将使其他人的生活变得地狱,因为操纵调用栈以隐藏您的函数将使每个人抓紧脑袋,并删除调试情况的宝贵证据。



这似乎是个好主意,但我不希望这样做。没错,而且我认为如果您将自己放在别人的鞋上,您将会理解尝试使用这种行为的函数会给他们的生活带来多大的困难。


Assume I have the following script:

def do_not_call_on_one(i):
    if i == 1:
        raise ValueError("You never listen.")

    print "Success!"

do_not_call_on_one(1)

On excution, you will see the following traceback:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    do_not_call_on_one(1)
  File "test.py", line 3, in do_not_call_on_one
    raise ValueError("You never listen.")
ValueError: You never listen.

Is there some way to manipulate the call stack, such that the error is emitted from the line which is actually causing the problem, like so?:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    do_not_call_on_one(1)
ValueError: You never listen.

This would save developer time that would otherwise be wasted scanning the call stack, searching for the incorrectly used function, when the correct behavior could be defined ahead of time.

Is there anything in Python that would allow an exception to use a modified traceback?

Update

There are buitins which are replicating this functionality:

# In test.py:
int('a')

# Executing 'python test.py' yields:
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    int('a')
ValueError: invalid literal for int() with base 10: 'a'

Note: The traceback does not descend into the int() function to display a bunch of useless scopes (especially the unhelpful raise ValueError itself).

解决方案

tl;dr: probably yes, but I can't imagine that it's a good idea.

I'm going to assume that we could, if we tried hard enough, fake up a callstack to show an exception coming from a different place. But I don't think that's a good idea.

In the first place, it's generally understood that the function which raises an exception is not always at fault. If somebody broke the contract and passed you a parameter which you don't expect, it's okay to raise an exception. If this is intended to cover your ass by raising the exception in the caller's scope so nobody blames your function, I think that somebody (maybe you, maybe your automated testing's "blame" system) needs to re-think how they determine responsibility.

In the second place, I don't think you can define the "right" scope very well. You might imagine that it should always be raised in your caller's scope, because clearly it's not your fault. Well, what if it's not their fault either? Should every function just throw up their hands and say "wasn't my fault" when an exception occurs? Pretty soon our stacktraces will say nothing at all.

Even if you're right, and your function is blameless, you're about to make everybody else's life hell, because manipulating the callstack to hide your function will make everybody scratch their heads, and remove valuable evidence for debugging the situation.

This might seem like a good idea, but I don't think it is, and I think if you put yourself in someone else's shoes you could understand how difficult it would make their life to try to use a function that behaved this way.

这篇关于从调用者的范围引发异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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