Gateworker要如何正确使用异常捕捉?

scan_z

本地win10系统测试:
在异步进程处理curl访问的时候捕捉异常必须用 catch (\Error $e)才能捕捉到,
在外面代码捕捉mysql异常必须 catch (\Exception $e) 才能捕捉到,

请问要如何正确使用这种catch捕捉异常?我线上环境PHP版本和本地都一样,就是系统不一样会影响使用方式不?

2534 2 0
2个回答

抽不完的寂寞

我觉得和系统没关系,和GatewayWorker也没关系。这个是php的语法,主要看php版本,php版本一样,使用方式就一样

  • scan_z 2020-05-27

    我是同PHP版本同代码测试的,使用方式不一样。

抽不完的寂寞

Error是 PHP 7 的新特性

即,PHP 7 开始,Error 与 Exception 都是继承自 Throwable。

从 Throwable 的继承关系,可以看到 Error 与 Exception 是平级的关系。

interface Throwable
  |- Error implements Throwable
      |- ArithmeticError extends Error
          |- DivisionByZeroError extends ArithmeticError
      |- AssertionError extends Error
      |- ParseError extends Error
      |- TypeError extends Error
          |- ArgumentCountError extends TypeError
  |- Exception implements Throwable
      |- ClosedGeneratorException extends Exception
      |- DOMException extends Exception
      |- ErrorException extends Exception
      |- IntlException extends Exception
      |- LogicException extends Exception
          |- BadFunctionCallException extends LogicException
              |- BadMethodCallException extends BadFunctionCallException
          |- DomainException extends LogicException
          |- InvalidArgumentException extends LogicException
          |- LengthException extends LogicException
          |- OutOfRangeException extends LogicException
      |- PharException extends Exception
      |- ReflectionException extends Exception
      |- RuntimeException extends Exception
          |- OutOfBoundsException extends RuntimeException
          |- OverflowException extends RuntimeException
          |- PDOException extends RuntimeException
          |- RangeException extends RuntimeException
          |- UnderflowException extends RuntimeException
          |- UnexpectedValueException extends RuntimeException

如果你用的是php7,直接 catch (\Throwable $e) 就行。否则Error 和 Exception 你就要2个都要catch

年代过于久远,无法发表回答
🔝