Prepends an exception handler to the stack.

@param string          $exceptionType Exception type
@param string|\Closure $handler       Exception handler
960069
public function handle(string $exceptionType, $handler): void
	{
		array_unshift($this->handlers, ['exceptionType' => $exceptionType, 'handler' => $handler]);
	}
119292
public function setErrorHandler(callable $handler, $types = 'use-php-defaults')
    {
        // Workaround for PHP 5.5
        if ($types === 'use-php-defaults') {
            $types = E_ALL | E_STRICT;
        }
        #set_error_handler([$this, 'onError'], error_reporting());
        return set_error_handler($handler, $types);
    }
120915
public function setExceptionType(string $exceptionType): self
    {
        try {
            $ref = new \ReflectionClass($exceptionType);
            if (!$ref->implementsInterface(\Throwable::class)) {
                throw new \InvalidArgumentException('Exception class must implement Throwable interface');
            }
        } catch (\ReflectionException $e) {
            throw new \InvalidArgumentException('Exception class not found');
        }

        $this->exceptionType = $exceptionType;
        return $this;
    }
1321922
public function attachHandler(callable $handler, $key = null)
    {
        if ($key !== null) {
            $this->eventHandlers[$key] = $handler;
        } else {
            $this->eventHandlers[] = $handler;
        }
    }
584665
public function exceptionHandler($exception) {
		try {
			// load errorhandler
			$debug = Factory::load('Debug');
			$debug->errorhandler($exception);
		} catch (\Exception $e) {
			echo "<pre>$exception</pre>\n\n";

			// useful if the \Exception handler itself contains errors
			echo "<pre>The Debug class threw an exception:\n$e</pre>";
		}
	}
775954
public function importResponseExceptionHandler(HandlerInterfaceException $handler)
    {
        $this->setCustomMessage('Error in request.');
        $this->data = array_merge($this->data, $handler->export());
        
        return $this;
    }
650680
def rescue_with_handler(exception)
      if handler = handler_for_rescue(exception)
        handler.arity != 0 ? handler.call(exception) : handler.call
        true # don't rely on the return value of the handler
      end
    end
261892
public function detach($eventType, $handler)
    {
        if (!isset($this->listeners[$eventType])) {
            return;
        }

        if (is_object($this->listeners[$eventType])) {
            // @attention PriorityQueue不支持移除
            $newPriorityQueue = new PriorityQueue();
            $newPriorityQueue->setExtractFlags(PriorityQueue::EXTR_DATA);

            /** @var PriorityQueue $oldPriorityQueue */
            $oldPriorityQueue = &$this->listeners[$eventType];
            $oldPriorityQueue->setExtractFlags(PriorityQueue::EXTR_BOTH);
            $oldPriorityQueue->top();

            foreach ($oldPriorityQueue as $currHandler) {
                if ($currHandler['data'] !== $handler) {
                    $newPriorityQueue->insert($currHandler['data'], $currHandler['priority']);
                }
            }

            $this->listeners[$eventType] = $newPriorityQueue;
        } else {
            if (($idx = array_search($handler, $this->listeners[$eventType], true)) !== false) {
                unset($this->listeners[$eventType][$idx]);
            }
        }
    }
809093
public static function register(self $handler = null, $replace = true)
    {
        if (null === self::$reservedMemory) {
            self::$reservedMemory = str_repeat('x', 10240);
            register_shutdown_function(__CLASS__.'::handleFatalError');
        }

        if ($handlerIsNew = null === $handler) {
            $handler = new static();
        }

        if (null === $prev = set_error_handler([$handler, 'handleError'])) {
            restore_error_handler();
            // Specifying the error types earlier would expose us to https://bugs.php.net/63206
            set_error_handler([$handler, 'handleError'], $handler->thrownErrors | $handler->loggedErrors);
            $handler->isRoot = true;
        }

        if ($handlerIsNew && \is_array($prev) && $prev[0] instanceof self) {
            $handler = $prev[0];
            $replace = false;
        }
        if (!$replace && $prev) {
            restore_error_handler();
            $handlerIsRegistered = \is_array($prev) && $handler === $prev[0];
        } else {
            $handlerIsRegistered = true;
        }
        if (\is_array($prev = set_exception_handler([$handler, 'handleException'])) && $prev[0] instanceof self) {
            restore_exception_handler();
            if (!$handlerIsRegistered) {
                $handler = $prev[0];
            } elseif ($handler !== $prev[0] && $replace) {
                set_exception_handler([$handler, 'handleException']);
                $p = $prev[0]->setExceptionHandler(null);
                $handler->setExceptionHandler($p);
                $prev[0]->setExceptionHandler($p);
            }
        } else {
            $handler->setExceptionHandler($prev);
        }

        $handler->throwAt(E_ALL & $handler->thrownErrors, true);

        return $handler;
    }
1244747
public void addException(ClassConstant type, int start, int end, int handler)
  {
    _exceptions.add(new ExceptionItem(type.getIndex(), start, end, handler));
  }
267240
public function exceptionHandler($e) {
        if (method_exists($e, 'getType')) {
            $this->addError($e->getMessage(), $e->getFile(), $e->getLine(), $e->getType());
        }
        else {
            $this->addError($e->getMessage(), $e->getFile(), $e->getLine(), gettype($e));
        }
    }
661324
protected function addHandler($event, callable $handler)
    {
        if (!isset($this->handlers[$event])) {
            throw new Exception("No such event '$event'");
        }

        if (!is_callable($handler)) {
            throw new Exception("Event handlers must be callable");
        }

        $this->handlers[$event][] = $handler;

        //Return last key
        return count($this->handlers[$event]) - 1;
    }
173203
public function exception(string $exception, $closure)
    {
        $this->exceptionHandler->register($exception, $this->getCallable($closure));
    }
1159820
public function triggerHandler($type, $exception)
    {
        foreach ($this->handlers[$type] as $handler) {
            $result = call_user_func_array($handler, array($exception, $this->wei));
            if (true === $result) {
                return true;
            }
        }
        return false;
    }
1089611
protected function ignoreException(Closure $closure, $defaultValue = null)
    {
        try {
            $defaultValue = call_user_func($closure);
        } catch (Exception $exception) {
        }

        return $defaultValue;
    }
109970
final protected function setHandler($handler)
    {
        if (is_callable($handler, false)) {
            $this->handleStack = ['default' => $handler];
        } elseif (is_object($handler)) {
            $this->handler = $handler;
        } else {
            $this->name = $handler;
        }
    }
791131
public function addFatalErrorHandler($handler) {
        if ( ! $handler instanceof INativeErrorHandler && ! is_callable($handler)) {
            throw new InvalidHandlerType(
                s('Fatal error handler must be a callable or an instance of %s.',
                    INativeErrorHandler::class)
            );
        }

        if (is_callable($handler)) {
            $handler = $this->createNativeErrorHandler($handler);
        }

        $this->fatalErrorHandlers[] = $handler;
    }