When an exception is triggered, an error exception handling module similar to other languages is added to PHP5. Exceptions in PHP code can be thrown by throw statements and captured by catch statements. Code that requires exception handling must be put into... when an exception is triggered, it usually occurs: an error exception handling module similar to other languages is added to PHP5. Exceptions in PHP code can be thrown by throw statements and captured by catch statements. All codes that require exception handling must be placed in the try code block to capture possible exceptions. Each try must have at least one catch corresponding to it.
Multiple catch methods can capture exceptions generated by different classes. when the try code block does not throw an exception or the catch code cannot be found to match the Exception thrown, the PHP code will continue to be executed after the jump to the last catch. Of course, PHP allows a throw exception to be thrown again in the catch code block. When an exception is thrown, it is later (note: refers to the code block where the exception is thrown) and PHP will try to find the first catch that can match it. if an exception is not caught, it is useless to use set_exception_handler () for corresponding processing, PHP will generate a serious error and output the Uncaught Exception... (No exception is captured.
1. the hierarchical relationship of the exception class. the code is as follows:
Class NotFoundException extends Exception {}
Class InputException extends Exception {}
Class DBException extends Exception {}
2. configure a processor without capturing exceptions. the code is as follows:
getMessage()); elseif ($e instanceof DBException) exit($e->getMessage()); else exit($e->getMessage());}set_exception_handler('exception_uncaught_handler');?>
3. manually throw a DBException exception in the database connection code, but try... Catch to capture and process the exception. The exception will be handled by the PHP custom exception processor, exception_uncaught_handler () function:
ResConn = mysql_connect ($ CONFIGS ['DB _ host'], $ CONFIGS ['DB _ user'], $ CONFIGS ['DB _ pwd']); if (false = is_resource ($ this-> resConn) throw new DBException ('database connection failed. '. Mysql_error ($ this-> resConn);?>
4. Business logic:
If (0! = Strcmp ($ curAlbum-> interest_id, $ it ))
Throw new NotFoundException ('sorry, the album you accessed does not exist ');
The above describes how to use a PHP custom exception processor.
The php instance code is as follows:
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.
1. the customException () class is created as an extension of the old exception class, so that it inherits all the attributes and methods of the old class.
2. create the errorMessage () function. if the email address is invalid, this function returns an error message.
3. execute the "try" code block. under the first condition, no exception is thrown.
4. because e-mail contains the string "example", the second condition triggers an exception.
5. the "catch" code block captures exceptions and displays appropriate error messages.
If the base mexception is not captured and the base exception is tightly captured, the exception is processed and thrown again. sometimes, when the exception is thrown, you may want to handle it in a different way than the standard. you can throw an exception again in a "catch" code block. the code is as follows:
Summary: PHP exceptions can be used in three steps:
Step 1: define the exception class. if not defined, use the default exception class;
Step 2: throw an exception when an exception occurs, such as ex1 ($ num2). The exception parameter $ num2 is obtained using the getMessage () of the exception;
Step 3: trigger an exception. use the try clause to throw new ex1 ($ num) when the condition is met );
Step 4: catch (ex2 $ e), equivalent to instantiating a defined exception class ex2 is $ e;
Note: Multiple exceptions can be defined, but only one exception can be triggered. that is to say, only one exception can be caught with catch.
Basic exception class. create a function that throws an exception:
1) {// Exception throw condition $ msg = "The value cannot exceed 1"; // Exception message throw new Exception ($ msg ); // throw an exception} echo "value less than 1";} // trigger an exception in the "try" code block try {num (3); echo "execution Normal ";} // catch Exception catch (Exception $ e) {echo "error message :". $ e-> getMessage (); // system method of Exception () to obtain Exception information echo "error File :". $ e-> getFile (); // system method of Exception () to obtain the abnormal file name echo "number of rows :". $ e-> getLine (); // Exception () system method to obtain the number of abnormal rows} // ================================== ============================================================ echo"
========================================================== ======================
"; // Extended basic exception class function checkEmail ($ email) {// defines a function that can throw an exception to determine the validity of the EMAIL if (filter_var ($ email, FILTER_VALIDATE_EMAIL) = false) {throw new checkEmailException ($ email); // if an exception is thrown, use EMAIL as the parameter} echo "the email is valid ";} class checkEmailException extends Exception {// defines the extension Exception class public function errormsg () {$ msg = "error cause :". $ this-> getMessage (). "It is not a legal EMAIL address! "; $ Msg. = "error File name :". $ this-> getFile (); $ msg. = "number of error rows :". $ this-> getLine (); echo $ msg; }}$ email = "email ..... @ Chhua.com "; try {// trigger the exception checkEmail ($ email);} // catch the exception catch (checkEmailException $ e) {$ e-> errormsg ();} /// ========================================================= multiple exceptions capture echo"
========================================================== ==============
"; Class ex1 extends Exception {// defines an Exception class public function msg () {$ msg =" error cause: ". $ this-> getMessage ()." greater than 100
"; $ Msg. =" error File: ". $ this-> getFile ()."
"; $ Msg. =" error code: ". $ this-> getCode ()."
"; $ Msg. =" number of rows: ". $ this-> getLine ()."
"; Echo $ msg;} class ex2 extends Exception {// defines an Exception class public function msg () {$ msg =" error cause :". $ this-> getMessage (). "equal to 100
"; $ Msg. =" error File: ". $ this-> getFile ()."
"; $ Msg. =" number of rows: ". $ this-> getLine ()."
"; Echo $ msg ;}$ num2 = 100; try {if ($ num2> 100) {// throw new ex1 ($ num2) triggered when the condition is met );} if ($ num2 = 100) {// triggers throw new ex2 ($ num2) when the condition is met;} catch (ex2 $ e) {// capture the triggered exception $ e-> msg ();} catch (ex1 $ e) {// capture the triggered exception $ e-> msg () ;}?>
Article address:
Reprint at will ^ please include the address of this article!