php try catch throw 用法

php try catch throw 用法

概述

  • try catch 捕捉不到fatal error致命错误
  • 只有抛出异常才能被截获,如果异常抛出了却没有被捕捉到,就会产生一个fatal error
  • 父类可以捕获子类抛出的异常
    class ExceptionNew extends Exception{
    }
    
    function try_throw($type) {
    if ($type == 1) {
            throw new ExceptionNew("sunyue");
    }
    }
    
    try {
        try_throw(1);
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    
  • try中的代码一旦抛出异常,代码将停止运行,直接执行catch中的代码
  • 多个catch捕获多个异常[PHP将查询一个匹配的catch代码块。如果有多个catch代码块,传递给每一个catch代码块的对象必须具有不同类型(或者可以用同一父类去捕获,这样只要一个catch就可以了),这样PHP可以找到需要进入哪一个catch代码块。当try代码块不再抛出异常或者找不到catch能匹配所抛出的异常时,PHP代码就会在跳转最后一个catch的后面继续执行。每次其实只能成功抛出一个异常,一个异常被捕获。]
    class ExceptionNew extends Exception{
    }
    class MyException extends Exception{
    }
    function try_throw($type) {
        if ($type == 1) {
            throw new ExceptionNew("sun");
        }
        if($type > 0){
            throw new MyException("yue");
        }
    }
    try {
        try_throw(1);
        try_throw(2);
    }catch (ExceptionNew $e) {
        echo $e->getMessage();
        echo "----ExceptionNew";
    }catch (MyException $e) {
        echo $e->getMessage();
        echo "----MyException";
    }
    //output
    //sun----ExceptionNew
    
  • php7新增Throwable Interface
  • Note that other types of errors such as warnings and notices remain unchanged in PHP 7. Only fatal and recoverable errors throw exceptions.
  • To unite the two exception branches, Exception and Error both implement a new interface, Throwable.
    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
    
  • If Throwable was defined in PHP 7 code, it would look like the code below.
    interface Throwable
    {
        public function getMessage(): string;
        public function getCode(): int;
        public function getFile(): string;
        public function getLine(): int;
        public function getTrace(): array;
        public function getTraceAsString(): string;
        public function getPrevious(): Throwable;
        public function __toString(): string;
    }
    
  • Throwable can be extended to create package-specific interfaces or add additional methods. An interface extending Throwable can only be implemented by a class extending either Exception or Error.
    interface MyPackageThrowable extends Throwable {}
    
    class MyPackageException extends Exception implements MyPackageThrowable {}
    
    throw new MyPackageException();
    
  • To catch any exception in PHP 5.x and 7 with the same code, multiple catch blocks can be used, catching Throwable first, then Exception. Once PHP 5.x support is no longer needed, the block catching Exception can be removed.
    try {
    
    // Code that may throw an Exception or Error.
    } catch (Throwable $t) {
    
    // Executed only in PHP 7, will not match in PHP 5
    } catch (Exception $e) {
    
    // Executed only in PHP 5, will not be reached in PHP 7
    }
    

参考链接

posted @ 2018-05-24 08:41  绝技小嗨皮  阅读(35588)  评论(0编辑  收藏  举报
Title