gpt4 book ai didi

ruby - 为什么在从堆栈跟踪中删除当前方法的情况下引发异常?

转载 作者:数据小太阳 更新时间:2023-10-29 08:01:08 24 4
gpt4 key购买 nike

当引发 Ruby 异常时,我知道我可以使用 caller 方法来生成删除当前方法调用的回溯:

raise SomeException, "error message", caller

根据镐书,这“在库模块中经常有用”。

为什么要从堆栈跟踪中删除当前方法?似乎关于错误发生的确切位置的信息越多,追踪起来就越容易。

最佳答案

对我来说似乎有用的原因是堆栈跟踪可以最清楚地将库的用户指向问题的根源。我的 Programming Ruby 没有提供比你的更多的见解,但是 The Ruby Programming Language 证实了我的感受:

The intent of the exception we're raising here is to point out a problem with the invocation of the ... method, not with the code inside the method. ... If we want to point directly to the problem code, we can provide a custom stack trace as the third argument to raise with ... caller.

关于ruby - 为什么在从堆栈跟踪中删除当前方法的情况下引发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11286879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com