Php exception handling methods. The following describes how to handle exceptions in php. For more information about how to handle exceptions, see this tutorial. The following is a reference snippet: 1. allow a method to provide an introduction to the exception handling methods in php. For more information about how to handle exceptions, see this tutorial.
The following is a reference clip:
1. allow a method to give an error mark to the customer code
2. provide detailed information about program errors
3. let you determine multiple error conditions at the same time and separate your error report from the program processing process.
4. the return value must be of an independent type and will not be confused with the normal returned type.
Two instances master PHP exception handling
// Example [1] use try... catch
The code is as follows: |
|
/* PDO connects to the mysql database. if you have not read the PDO, first check the PDO constructor. otherwise, skip example 1 and see Example 2 */ $ Dsn = 'MySQL: host = localhost; dbname = testdb '; $ User = 'dbuser '; $ Password = 'dbpass '; Try { $ Dbh = new PDO ($ dsn, $ user, $ password); // An exception may occur when you create a database connection object. Echo 'If an exception occurs above, I cannot be displayed '; } Catch (PDOException $ e ){ Echo 'connection failed: '. $ e->__ toString (); } ?> |
Example
The code is as follows: |
|
// Example [2] try... cathc and throw Try { $ Error = 'I throw exception information and exit the try block '; If (is_dir ('./tests ')){ Echo 'Do something .'; } Else { Throw new Exception ($ error, 12345 ); } Echo 'if there is an exception above, it won't turn to me !~ ', "N "; } Catch (Exception $ e ){ Echo 'capture exception: ', $ e-> getMessage (), $ e-> getCode (), "n "; // Display $ error and 123456 } Echo 'continue execution '; ?> |
As a friend said above, I wrote some program exercises for myself:
The following is a code snippet:
The code is as follows: |
|
$ A = 20;
// Example 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" -------------------------------------------------------------------------------- ";
// Example 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 clip:
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}
Bytes. The following is a reference segment: 1. allow a method to provide an output...