PHP Fatal error: Uncaught exception 'Exception'

后端 未结 3 1433
名媛妹妹
名媛妹妹 2020-12-17 19:25

I\'m playing around with exceptions in PHP. For example, I have a script that reads a $_GET request and loads a file; If the file doesn\'t exists, an new exception should be

3条回答
  •  情话喂你
    2020-12-17 20:13

    This is expected behavior for an uncaught exception with display_errors off.

    Your options here are to turn on display_errors via php or in the ini file or catch and output the exception.

     ini_set("display_errors", 1);
    

    or

     try{
         // code that may throw an exception
     } catch(Exception $e){
         echo $e->getMessage();
     }
    

    If you are throwing exceptions, the intention is that somewhere further down the line something will catch and deal with it. If not it is a server error (500).

    Another option for you would be to use set_exception_handler to set a default error handler for your script.

     function default_exception_handler(Exception $e){
              // show something to the user letting them know we fell down
              echo "

    Something Bad Happened

    "; echo "

    We fill find the person responsible and have them shot

    "; // do some logging for the exception and call the kill_programmer function. } set_exception_handler("default_exception_handler");

提交回复
热议问题