letsface/php-error-handler

为PHP提供异常处理程序,用于根据特定的异常代码自定义行为

v1.2 2015-07-24 03:59 UTC

This package is not auto-updated.

Last update: 2024-10-02 10:17:06 UTC


README

为PHP提供异常处理程序,用于根据特定的异常代码自定义行为

使用方法

自定义处理程序

$handler = new \Letsface\ExceptionHandler();
$handler->listen('123', function() {
  return 'foo';
});

try {
  throw new \MyException('', '123');
}
catch(\Exception $e) {
  // will echo "foo";
  echo $handler->handle($e);
}

抛出异常

$handler = new \Letsface\ExceptionHandler();
$handler->throws('123', new \AnotherException('My new message', 'NEWCODE'));

try {
  throw new \MyException('', '123');
}
catch(\Exception $e) {
  // will throw "\AnotherException";
  $handler->handle($e);
}

重新抛出异常

$handler = new \Letsface\ExceptionHandler();
$handler->rethrow('123');

try {
  throw new \MyException('', '123');
}
catch(\Exception $e) {
  // will throw $e;
  $handler->handle($e);
}