PHP exceptions and error handling mechanisms

Source: Internet
Author: User
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:

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.