cause === NULL) { $this->cause = $cause; } else { throw new BadMethodCallException('Cause was already assigned'); } } /** * Gets the Exception instance that caused the current exception * * @return Exception */ public function getCause() { return $this->cause; } /** * Returns string represenation of exception * * @return string */ public function __toString() { return parent::__toString() . ($this->cause === NULL ? '' : "\nCaused by " . $this->cause->__toString()); } /** * Enables converting all PHP errors to exceptions * * @param Exception class to be thrown * @return void */ public static function catchError($class = __CLASS__) { self::$oldHandler = set_error_handler(array(__CLASS__, '_errorHandler'), E_ALL); self::$handlerClass = $class; } /** * Disables converting errors to exceptions * * @return void */ public static function restore() { if (self::$oldHandler !== NULL) { set_error_handler(self::$oldHandler); self::$oldHandler = NULL; } else { restore_error_handler(); } } /** * Internal error handler */ public static function _errorHandler($code, $message, $file, $line, $context) { self::restore(); if (ini_get('html_errors')) { $message = strip_tags($message); } throw new self::$handlerClass($message, $code); } }