Analysis of PHP exception handling _php

Source: Internet
Author: User
Keywords PHP exception handling
Tags php exception handling
PHP is scheduled for two exception classes: Exception and Errorexception

The code is as follows:


Exception {
/* Properties */
protected string $message; Exception message Content
protected int $code; Exception code number
protected string $file; The file name that throws the exception
protected int $line; Throws the line number of the exception in the file
/* Method */
Public __construct ([string $message = "" [, int $code = 0 [, Exception $previous = null]])
Final public string getMessage (void)//exception-thrown information
Final public Exception getprevious (void)//Previous exception
Final public int getcode (void)//exception code, which is user-defined
Final public string getFile (void)//unexpected file path strength
Final public int getLine (void)//The row where the exception occurred
Final public array gettrace (void)//exception tracking information (array)
Final public string gettraceasstring (void)//Exception tracking information (string)
public string __tostring (void)//The return value of the child function is called when attempting to use the exception object as a string directly
Final private void __clone (void)//Clone exception object when called
}

The code is as follows:


Errorexception extends Exception {

/* Properties */
protected int $severity;
/* Method */

Public __construct ([string $message = "" "[, int $code = 0 [, int $severity = 1 [, String $filename = __file__ [, int $li Neno = __line__ [, Exception $previous = NULL]] []])
Final public int getseverity (void)
/* Inherited method */
Final public string exception::getmessage (void)
Final public Exception exception::getprevious (void)
Final public int exception::getcode (void)
Final public string exception::getfile (void)
Final public int exception::getline (void)
Final public array exception::gettrace (void)
Final public string exception::gettraceasstring (void)
public string exception::__tostring (void)
Final private void exception::__clone (void)
}

So how do I catch an exception?

(1) PHP available try...catch ... To catch an exception, the code for exception handling must be inside a try code block.

The code is as follows:


try {
throw new Exception (' Exception Test 1 ', 1001);
} catch (Exception $e) {
echo $e->getmessage (). ' -'. $e->getcode ();
}

(2) The user can customize the exception handling function [Set_exception_handler], which is used for exceptions caught with Try/catch.

The code is as follows:


function Exception_handler ($e) {
echo "uncaught exception:", $e, GetMessage (), "\ n";
}

Set_exception_handler (' Exception_handler ');

throw new Exception (' uncaught Exception ');

echo "This line will not be executed";

You can see that the exception is handled using the Ser_exception_handler callback function, and subsequent code does not continue, but Try-catch can.
(3) PHP can catch different types of exceptions with multiple catch and allow the exception to be thrown again within the catch code block.

The code is as follows:


Please extend the exception class according to the actual
Class MyException extends Exception {
Public function __construct ($message = ", $code = 0) {

}

Public Function myFunction () {
Echo ' Just for test ';
}
}

try {
throw new MyException (' an error ');
} catch (MyException $e) {
echo $e->myfunction ();
} catch (Exception $e) {
echo $e->getmessage ();
}


(4) PHP5.5 has supported the FINALLY keyword, you do not have to worry about whether the exception overflow.

Can be compared as follows:

The code is as follows:


function dosomething () {
$resource = Createresource ();
try {
$result = Useresource ($resource);
} catch (Exception $e) {
Releaseresource ($resource);
Log ($e->getmessage ());
Exit ();
}
Releaseresource ($resource);
return $result;
}

After using finally
function DoSomething2 () {
$resource = Createresource ();
try {
$result = Useresource ($resource);
return $result;
} catch (Exception $e) {
Log ($e->getmessage ());
Exit ();
} finally {
Releaseresource ($resource);
}
}

  • 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.