【问题标题】:How to point all notices and warnings to exceptions?如何将所有通知和警告指向异常?
【发布时间】:2013-10-21 23:02:02
【问题描述】:

是否可以在异常中指出try/catch 内的所有通知和警告。但是只在 try 中这样做,其余的代码应该使用默认的错误处理。主要问题是 try 在 while 循环中并且可以被多次调用,所以我认为多次使用 set_error_handlerrestore_error_handler 不是一个好主意。也许php在java中有类似的东西?

【问题讨论】:

    标签: php exception-handling


    【解决方案1】:

    唯一的方法是使用错误处理程序。但是您可以配置错误处理程序来抛出或不抛出异常:

    class ErrorHandler {
      public static $throw = false;
    
      public static function handler() {
        if(self::$throw) throw new Exception();
      }
    }
    
    set_error_handler(array('ErrorHandler', 'handler'));
    ErrorHandler::$throw = true;
    

    【讨论】:

      猜你喜欢
      • 2015-12-25
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      • 2021-06-11
      • 1970-01-01
      • 2011-08-04
      • 1970-01-01
      相关资源
      最近更新 更多