Try/Catch block in PHP not catching Exception

后端 未结 12 792
既然无缘
既然无缘 2020-12-04 13:45

I am trying to run this Example #1 from this page: http://php.net/manual/en/language.exceptions.php



        
12条回答
  •  [愿得一人]
    2020-12-04 14:38

    My initial though is you have a typo in the name of the exception you are catching/throwing, but if your code is exactly the same I'm not sure exactly what is going on.

    Try the following modification of the original script, and paste your results. It will help diagnose your issue a bit better.

    getMessage(), "\n";
    }
    
    //install the handler
    set_exception_handler('exception_handler');
    
    class MyException extends Exception {
    }
    
    function inverse($x) {
        if (!$x) {
            throw new MyException('Division by zero.');
        }
        else return 1/$x;
    }
    
    try {
        echo inverse(5) . "\n";
        echo inverse(0) . "\n";
    } catch (MyException $e) {
        echo 'Caught myexception: ',  $e->getMessage(), "\n";
    } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }
    
    // Continue execution
    echo 'Hello World';
    ?>
    

提交回复
热议问题