doufuhuang6981 2013-04-21 02:42
浏览 18

如何显示导致异常的行,而不是异常发生的位置?

If a function throws an exception because the calling code does not call it properly, how do I make the error show the file and line number of the calling code instead of (or in addition to) the line where the exception is thrown?

ie:

class MyClass {
   public function __call($MethodName, $Parameters)
   {
       if (!property_exists($this, $PropertyName))            
         throw new Exception('Error Getting Property: ' . $PropertyName . ' does not exist!');
       // THIS line number appears in the exception, but
       // is useless because it isn't the problem.
   }
}


$MyClass = new MyClass();
// THIS line number should be in the exception,
// since its the line that's wrong.
$MyClass->GetSomethingThatDoesNotExist(); 
  • 写回答

1条回答 默认 最新

  • douyalin2258 2013-04-21 03:35
    关注

    There's a few options for this;

    Roll your own

    Using the debug_print_backtrace() function in PHP you can make something to display your own output.

    Pre built libraries to output the stack trace

    You can also install the awesome whoops package to get a nicer output of the problem and how the code got there, check it out here: https://github.com/filp/whoops

    Xdebug

    This is the preferred due to better var_dump() output among other huge advantages. Once installed, it just makes all your debugging and error output much more insightful and easier to read.

    Install xdebug to display a more verbose output of the whole flow of events that lead to the issue, known as the stack trace.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭