PHP has ordered two exception classes: Exception and Errorexception
Copy Code code as follows:
Exception {
/* Properties * *
protected string $message; Exception message Contents
protected int $code; Exception code number
protected string $file; Name of the exception thrown
protected int $line; The line number that throws 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 in which 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)//To call the return value of a child function when an exception object is used directly as a string
The final private void __clone (void)//is called when the exception object is cloned
}
Copy Code code as follows:
Errorexception extends Exception {
/* Properties * *
protected int $severity;
/* Method * *
Public __construct ([string $message = "] [, int $code = 0 [, int $severity = 1 [, String $filename = __file__ [ , int $lineno = __line__ [, Exception $previous = NULL]]]]
Final public int getseverity (void)
/* Inheritance 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 can be try...catch ... To catch an exception, the code for exception handling must be within the try code block.
Copy Code code 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] for exceptions that are not captured with Try/catch.
Copy Code code 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 carried out";
You can see that using the Ser_exception_handler callback function to handle exceptions, subsequent code will not continue to execute, 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.
Copy Code code 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 finally keyword, you need not care about whether the exception overflow.
Can be compared as follows:
Copy Code code 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);
}
}