php - How to catch an unexpected error? -


i'm writing script, lot of things go wrong. i'm making if/else statements obvious things, heppen, there way catch something, possible heppen, don't know yet?

for example causes error of kind, in middle of script. want inform user, has gone wrong, without dozens of php warning scripts.

i need like

-- start listening && stop error reporting --  script  -- end listening --  if(something went wrong) $alert = 'oops, went wrong.'; else $confirm = 'everything fine.' 

thanks.

why not try...catch?

$has_errors = false;     try {   // code here  } catch (exception $e) {       // handle exception, or save later   $has_errors = true; }  if ($has_errors!==false)   print 'this did not work'; 

edit:

here sample set_error_handler, take care of error happens outside context of try...catch block. handle notices, if php configured show notices.

based on code from: http://php.net/manual/en/function.set-error-handler.php

set_error_handler('genericerrorhandler');  function genericerrorhandler($errno, $errstr, $errfile, $errline) {     if (!(error_reporting() & $errno)) {         // error code not included in error_reporting         return;     }      switch ($errno) {     case e_user_error:         echo "<b>my error</b> [$errno] $errstr<br />\n";         echo "  fatal error on line $errline in file $errfile";         echo ", php " . php_version . " (" . php_os . ")<br />\n";         echo "aborting...<br />\n";         exit(1);         break;      case e_user_warning:         echo "<b>my warning</b> [$errno] $errstr<br />\n";         break;      case e_user_notice:         echo "<b>my notice</b> [$errno] $errstr<br />\n";         break;      default:         echo "unknown error type: [$errno] $errstr<br />\n";         break;     }      /* don't execute php internal error handler */     return true; } $v = 10 / 0 ; die('here');  

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -