What is an exception? PHP5 provides a new object-oriented error handling method. Exception handling is used to change the normal process of the script when a specified error (Exception) occurs. if you are interested, refer to Exception) it is used to change the normal process of the script when a specified error occurs. What is an exception? PHP 5 provides a new object-oriented error handling method. Exception handling is used to change the normal process of the script when a specified error (exception) occurs. This is called an exception. When an exception is triggered, it usually occurs: the current code status is saved and code execution is switched to a predefined exception processor function, the processor may re-execute the code from the saved code status, terminate the script execution, or continue executing the script from another position in the code. we will display different error handling methods: basic exception: create a custom exception processor. multiple exceptions are thrown again. set the top-level exception processor.
Basic usage of exceptions when an exception is thrown, the subsequent code will not continue to be executed, and PHP will try to find the matching "catch" code block. If the exception is not captured and set_exception_handler () is not used for corresponding processing, a serious (fatal) error will occur ), and output the "Uncaught Exception" (No Exception is caught) error message. Let's try to throw an exception without capturing it:
1) { throw new Exception("Value must be 1 or below"); } return true; } //trigger exceptioncheckNum(2);?>
The above code will get an error similar to this: Fatal error: Uncaught exception 'exception' with message 'value must be 1 or below 'in C: \ webfolder \ test. php: 6 Stack trace: #0 C: \ webfolder \ test. php (12): checkNum (28) #1 {main} thrown in C: \ webfolder \ test. php on line 6 Try, throw, and catch to avoid errors in the above example, we need to create appropriate code to handle exceptions. The handler should include: Try-use abnormal functions should be located in the "try" code block. If no exception is triggered, the code continues as usual. However, if an exception is triggered, an exception is thrown. Throw-This specifies how to trigger an exception. Each "throw" must correspond to at least one "catch" Catch-"catch" code block to capture exceptions and create an object containing exception information.
Let's trigger an exception:
1) {throw new Exception ("Value must be 1 or below");} return true;} // trigger an Exception in the "try" code block try {checkNum (2 ); // If the exception is thrown, this text will not be shown echo 'If you see this, the number is 1 or below';} // catch an Exception catch (exception $ e) {echo 'message :'. $ e-> getMessage () ;}?>
The above code will get an error like this: Message: Value must be 1 or below. The example explains: The above code throws an exception and captures it:
Create the checkNum () function. It checks whether the number is greater than 1. If yes, an exception is thrown. Call the checkNum () function in the "try" code block. An exception in the checkNum () function is thrown. the "catch" code block receives the exception and creates an object containing the exception information ($ e ). Call $ e-> getMessage () from this exception object to output error messages from this exception.
However, to follow the "each throw must correspond to a catch" principle, you can set a top-level exception processor to handle missed errors. It is very easy to create a custom Exception handler for the custom Exception class. We have simply created a special class. When an exception occurs in PHP, we can call its function. This class must be an extension of the exception class.
This custom exception class inherits all attributes of the PHP exception class. you can add custom functions to it. Let's start to create the exception class:
getLine().' in '.$this->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(); }?>
This new class is a copy of the old exception class, plus the errorMessage () function. Because it is a copy of the old class, it inherits attributes and methods from the old class. we can use methods of the exception class, such as getLine () and getFile () and getMessage (). The preceding code throws an exception and captures it through a custom exception class: the customException () class is created as an extension of the old exception class. In this way, it inherits all the attributes and methods of the old class. Create the errorMessage () function. If the email address is invalid, this function returns an error message and sets the $ email variable to an invalid email address string to execute the "try" code block, because the email address is invalid, an exception "catch" code block is thrown to capture the exception and an error message is displayed.
Multiple exceptions can be used for a script to detect multiple exceptions. You can use multiple if. else code blocks, a switch code block, or nested exceptions. These exceptions can use different exception classes and return different error messages:
getLine().' in '.$this->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") !== FALSE) { throw new Exception("$email is an example e-mail"); } } catch (customException $e) { echo $e->errorMessage(); } catch(Exception $e) { echo $e->getMessage(); }?>
The preceding code tests two conditions. if any condition is invalid, an exception is thrown:
The customException () class is created as an extension of the old exception class. In this way, it inherits all the attributes and methods of the old class. Create the errorMessage () function. If the email address is invalid, this function returns an error message. Execute the "try" code block. under the first condition, no exception is thrown. Because e-mail contains the string "example", the second condition triggers an exception. The "catch" code block captures exceptions and displays appropriate error messages.
If the base mexception is not captured and the base exception is captured tightly, the exception is handled there.
If an exception is thrown again, you may want to handle it in a different way than the standard one. You can throw an exception again in a "catch" code block. The script should hide system errors from users. For programmers, system errors may be important, but users are not interested in them. To make it easier for users to use, you can throw an exception with user-friendly messages again:
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(); }?>
The preceding code checks whether the email address contains the string "example ". If yes, an exception is thrown again:
The customException () class is created as an extension of the old exception class. In this way, it inherits all the attributes and methods of the old class. Create the errorMessage () function. If the email address is invalid, this function returns an error message. Set the $ email variable to a valid email address, but it contains the string "example ". The "try" code block contains another "try" code block, so that an exception can be thrown again. An exception is triggered because e-mail contains the string "example. "Catch" captures this exception and throws "customException" again ". Capture "customException" and display an error message. If an exception is not caught in its current "try" code block, it looks for the catch code block at a higher level. You can set the Top-Level Exception Handler set_exception_handler () function to process all user-defined functions without capturing exceptions.
getMessage();}set_exception_handler('myException');throw new Exception('Uncaught Exception occurred');?>
The output of the above code should be similar to this: Exception: Uncaught Exception occurred. in the above code, the "catch" code block does not exist, but triggers the Exception handler at the top layer. This function should be used to capture all exceptions that are not captured.
Code that requires exception handling for exception rules should be placed in the try code block to capture potential exceptions. Each try or throw code block must have at least one catch code block. Multiple catch code blocks can capture different types of exceptions. You can throw a (re-thrown) exception again in the catch code block in the try code block.
In short: if an exception is thrown, it must be caught.
The above is an introduction to PHP Exception handling Exception classes. I hope it will be helpful for your learning.