Exception (Exception): Exception handling is used to change the normal process of a script when a specified error (exception) condition occurs.
When an exception is triggered, it usually occurs:
The current code state is being switched to the predefined exception handler function depending on the situation, the processor may restart execution of code from the saved code State, terminate the script execution, or resume execution of the script from another location in the code
We will show you different error handling methods:
Basic use of exceptions create custom exception handlers multiple exception re-throw exception set top-level exception handler
Basic use:
Try, throw and catch to avoid the error in the above example, we need to create the appropriate code to handle the exception. The processing handler should include: try-the function that uses the exception should be in the "Try" code block. If no exception is triggered, the code will continue to execute as usual. But the exception is triggered and an exception is thrown.
Throw-this specifies how the exception is triggered. Each "throw" must correspond to at least one catch catch-the "catch" code block catches the exception and creates a pair containing the exception information Like
getMessage (), "\ n";} Continue execution echo ' Hello world '; ?>
~ Create a custom Exception class
Creating a custom exception handler is straightforward. We have simply created a special class that can call its function when an exception occurs in PHP. The class must be an extension of the exception class.
This custom exception class inherits all the properties of the PHP exception class, and you can add custom functions to it.
We started to create the exception class:
GetLine (). ' In ' . $-- getFile (). ' ' . $ This , GetMessage (). ' is not a valid e-mail address ' ; return $ERRORMSG;}} $email = " someone@example...com " ; Try { // Check if if (Filter_var ($email, filter_validate_email) = = = = FALSE) { // Throw exception if email is not valid throw new customexception ($email);}} Catch (Customexception $e) { // display custom message echo $e -errormessage ();} ?>
~ Multiple Exceptions
You can use multiple exceptions for a script to detect multiple situations.
You can use multiple if: else code block, or a switch code block, or nested multiple exceptions. These exceptions can use different exception classes and return different error messages:
GetLine (). ' in '. $--getFile (). ' :'. $ this, getMessage (). ' is not a valid e-mail address '; return $ERRORMSG;}} $email = "someone@example.com"; try {//check if if (Filter_var ($email, filter_validate_email) = = = FALSE) {// Throw exception if email is not valid throw new Customexception ($email); }//Check for ' example ' in mail address if (Strpos ($email, "example")!== F Alse) {throw new Exception ("$email is an example e-mail");}} catch (Customexception $e) {echo $e-errormessage ();} catch (Exception $e) {echo $e-getMessage ();} ?>
~ Re-throw exception
Sometimes, when an exception is thrown, you might want to handle it in a different way from the standard. You can throw an exception again in a "catch" code block.
The script should hide the system error from the user. For programmers, system errors may be important, but users are not interested in them. To make it easier for users to use, you can again throw an exception with a friendly message to the user:
GetMessage (). ' is not a valid e-mail address. ' ; return $ERRORMSG;}} $email = " someone@example.com " ; Try { try { // Check for ' example ' in mail address if (Strpos ($email, " example " ) !== FALSE) { // Throw exception if email is not valid throw New Exception ($email); }} catch (Exception $e) {//re-throw Exception throw new customexception ($email);}} Catch (Customexception $e) { // display custom message echo $e -errormessage ();} ?>
~ Set the top level exception handler (top levels Exception Handler)
The Set_exception_handler () function sets a user-defined function to handle all uncaught exceptions.
Exception: ", $exception->getmessage ();} Set_exception_handler (' myexception '); throw new Exception (' uncaught exception occurred ');? >
The output of the above code should look like this:
Exception:uncaught Exception occurred
In the above code, there is no "catch" code block, but the top-level exception handler is triggered. You should use this function to catch all uncaught exceptions.
The exception rule code that requires exception handling should be placed inside a try code block to catch a potential exception. Each try or throw code block must have at least one corresponding catch code block. You can use multiple catch blocks to catch different kinds of exceptions. You can throw (re-thrown) exceptions again in a catch code block within a try code block.
In short: If an exception is thrown, it must be captured.
~