PHP only throws exceptions manually to catch exceptions
Class Emailexception extends exception{ //define Magic method, direct output object information public Function __tostirng () {$error = "code:{$this GetCode ()},message:{$this->getmessage ()} line:{$this->getline ()},file:{$this->getfile ()} "; return $error ;}} function Reg ($i) {if ($i >0) {throw new Emailexception ("Error");}} Try{reg ($i =6);} catch (Emailexception $e) {echo $e; echo $e->getmessage ();} catch (Exception $e) { $e->getmessage ();} It is important to note here that exception as a superclass should be put to the last capture//If this superclass is caught in advance, the subsequent capture terminates and does not provide targeted information processing
Run diagram
Custom exception handlers (only catch exceptions and non-fatal errors, and fatal errors will still hang)
function customerror ($errno, $errstr, $errfile, $errline) {echo " error code [${error}]${errstr}". "
"; echo" error code line: {$errline} file {$errfile} "."
"; echo" PHP version, Php_version, "(", Php_os, ")". "
";} Set_error_handler ("Customerror", e_all| E_STRICT); $a = array (' o ' =>2,4,6,8); echo $a [o]; The wrong Code//set_error_handler () function takes over PHP's built-in error handling,//Can be canceled on the same page using Restore_error_handler ()
Operation diagram:
Simple handling of fetal error errors
Class Shutdown{public function Stop () {if (Error_get_last ()) {Print_r (Error_get_last ());} Die (' Stop. ');}} Register_shutdown_function (Array (new shutdown (), ' Stop ')); This function triggers a function $ A = new A () when the PHP program terminates or die. Error code echo "Fatal error";
Operation diagram: