PHP从调用者抛出异常

时间:2013-11-09 13:23:36

标签: php exception

是否可以抛出一个似乎来自函数调用者而不是调用函数内部的异常。

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

/* ... */

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

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

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

1 个答案:

答案 0 :(得分:1)

改变这个:

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