本地win10系統(tǒng)測試:
在異步進程處理curl訪問的時候捕捉異常必須用 catch (\Error $e)才能捕捉到,
在外面代碼捕捉mysql異常必須 catch (\Exception $e) 才能捕捉到,
請問要如何正確使用這種catch捕捉異常?我線上環(huán)境PHP版本和本地都一樣,就是系統(tǒng)不一樣會影響使用方式不?
Error是 PHP 7 的新特性
即,PHP 7 開始,Error 與 Exception 都是繼承自 Throwable。
從 Throwable 的繼承關(guān)系,可以看到 Error 與 Exception 是平級的關(guān)系。
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