Here is a description of the exception handling in PHP introduction, there is a need to learn the exception handling friends can refer to this tutorial.
The following is a reference fragment:
1. Allows a method to give an error token to the customer code
2. Provider Error Details
3. Let you judge multiple error conditions at the same time, separating your error reporting from the program processing process.
4. The return value must be of a separate type and will not be confused with the normal return type
Two examples of PHP exception handling
Example "1" with Try...catch
The code is as follows |
Copy Code |
/* PDO connect to MySQL database, if you have not seen PDO, first look at the PDO constructor, or skip example 1 to see Example 2 */ $dsn = ' Mysql:host=localhost;dbname=testdb '; $user = ' Dbuser '; $password = ' Dbpass '; try { $DBH = new PDO ($DSN, $user, $password); Creating database connection objects is prone to exceptions Echo ' If there is an exception above it will not show me '; } catch (Pdoexception $e) { Echo ' Connection failed: '. $e->__tostring (); } ?> |
Cases
The code is as follows |
Copy Code |
example [2] Try: CATHC and throw together try { $error = ' I throw an exception message and jump out of the try Block '; if (Is_dir ('./tests ')) { Echo ' do sth. '; }else{ throw new Exception ($error, 12345); } Echo ' There's something wrong with it, it's not my turn! ~ ', ' n '; } catch (Exception $e) { Echo ' catch exception: ', $e->getmessage (), $e->getcode (), "N "; Show $error and 123456 } Echo ' continues to execute '; ?> |
One of the above friends said, the following I have to write a few procedures to practice:
Here is the code snippet:
The code is as follows |
Copy Code |
$a = 20;
Examples of using regular catch Try { if ($a = = 1) { throw new Exception ("I am 1", 1); } ElseIf ($a = = 2) { throw new Exception ("I am 2", 3); } ElseIf ($a = = 3) { throw new Exception ("I am 3", 3); } ElseIf ($a = = 4) { throw new Exception ("I am 4", 4); } Else { throw new Exception ("Who am I?", 0); } } catch (Exception $e) { if ($e->getcode () = = 1) { echo "1:" $E; } ElseIf ($e->getcode () = = 2) { echo "2:" $E; } ElseIf ($e->getcode () = = 3) { echo "3:" $E; } ElseIf ($e->getcode () = = 4) { echo "4:" $E; } Else { echo "0:" $E; } } echo " -------------------------------------------------------------------------------- ";
Examples of using different extended exception classes Class A extends exception{}; Class B extends exception{}; Try { if ($a = = 1) { throw new A ("I am A", 1); } ElseIf ($a = = 2) { throw new B ("I am B", 2); } Else { throw new Exception ("Who am I?", 0); } } catch (A $e) { echo "1:" $E; } catch (B $e) { echo "2:" $E; } catch (Exception $e) { echo "0:" $E; } ?>
|
The output is as follows:
The following is a reference fragment:
0:exception ' exception ' with message ' Who am I? ' in D:webphpwwwnewtest.php:24 Stack trace: #0 {main}
--------------------------------------------------------------------------------
0:exception ' exception ' with message ' Who am I? ' in d:webphpwwwnewtest.php:69 Stack trace: #0 {main}
http://www.bkjia.com/PHPjc/629027.html www.bkjia.com true http://www.bkjia.com/PHPjc/629027.html techarticle Here is a description of the exception handling in PHP introduction, there is a need to learn the exception handling friends can refer to this tutorial. The following is a reference fragment: 1. Allows a method to give a ...