This article illustrates the use of the Set_exception_handler function in thinkphp. Share to everyone for your reference. The specific methods are as follows:
Function:
Copy Code code as follows:
String Set_exception_handler (callback $exception _handler)
Definition and Usage:
The Set_exception_handler () function sets the user-defined exception-handling function.
This function is used to create the user's own exception handling methods for the Run-time period.
The function returns the old exception handler, or null if it fails.
Grammar:
Set_exception_handler (exception_function)
Parameters |
Description |
Error_function |
Necessary. A function that is called when an exception that is not caught is specified. The function must be defined before calling the Set_exception_handler () function. This exception handler requires a parameter, the exception object that is thrown. |
Tips and Comments:
tip : After this exception handler is invoked, the script stops executing.
In the same time to see the TP code found this function, I do not understand, I have not cared about these things before? (Wringing ... )
Continue to see how TP is implemented, uh, why must use TP, hmm. Well, I'll take the example of the handbook in a moment.
Copy Code code as follows:
Public Function appexception ($e)
{
Halt ($e->__tostring ());
}
Set_exception_handler (Array (& $this, "Appexception"));
Oh, this simple? Because I did not give the implementation of halt this method, this is enough.
And look at the examples in the manual,
Copy Code code as follows:
function Exception_handler ($exception) {
echo "uncaught exception:", $exception->getmessage (), "n";
}
Set_exception_handler (' Exception_handler ');
throw new Exception (' uncaught Exception ');
echo "Not Executedn";
In fact, Set_exception_handler is set to call which custom function when your program needs to throw an exception.
It's that simple.
Precautions:
Set_exception_handler ("MyException") can accept not only functions but also methods of the class (both exposed static methods and exposed non-static methods), but it needs to be passed as an array, the first value of the array is "class name" and the second parameter is "Method Name"
I hope this article will be helpful to everyone's thinkphp framework program design.