A _php instance of the use of Set_exception_handler function in thinkphp

Source: Internet
Author: User
Tags exception handling

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.

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.