2

是否有可能抛出似乎源自函数调用者而不是被调用函数内部的异常。

function assert_foo(){
  throw new CustomException();
}

/* ... */

assert_foo(); // <-- I want the exception to seem to originate from here */

assert_foo函数仅用于测试代码,其中测试框架仅显示异常源自的位置,而不是完整的调用堆栈。唯一的逻辑assert_foo是验证状态。

我试图覆盖 Exception::getTrace 但是Cannot override final method Exception::getTrace().

4

1 回答 1

2

改变这个:

function assert_foo()
{
   throw new CustomException();
}

/* ... */

assert_foo(); 

至:

function assert_foo()
{
   throw new CustomException();
}

/* ... */

try
{
    assert_foo(); //AAA
}
catch(CustomException $ce )
{
  // create new and throw from here 
  throw new CustomException();
}

现在你有你想要的,即。您的异常将被视为来自AAA

于 2013-11-09T13:29:16.937 回答