In program development, error handling is very important. today this article tells him about the importance of error processing functions in php development and provides examples.
In program development, error handling is very important. today this article tells him about the importance of error processing functions in php development and provides examples.
1. built-in exception handling class, 2. Examples of capturing and handling exceptions, 3. getmessage (), 4. getfile (), a member function of the exception class () 5. getline (), 6, display warning or error messages, 7. Custom error handling functions for the exception class.
1. built-in exception handling class
- Class exception
- {
- Protected $ message = 'unknown exception'; // exception information
- Protected $ code = 0; // custom exception code
- Protected $ file; // file name with an exception
- Protected $ line; // The code line number with an exception
- Function _ construct ($ message = null, $ code = 0 );
- Final function getmessage (); // returns exception information
- Final function getcode (); // returns the exception code
- Final function getfile (); // returns the file name with an exception
- Final function getline (); // return the code line number in which an exception occurs.
- Final function gettrace (); // backtrace () array
- Final function gettraceasstring (); // gettrace () information formatted as a string
- Function _ tostring (); // output string
- }
2. example of capturing and handling exceptions
- Try
- {
- $ Error = 'throws exception information and jumps out of the try block.
';
- If (is_dir ('./test '))
- {
- Echo '../ch16 is a directory ';
- Echo'
';
- Echo 'other operations may be continued ';
- Echo'
';
- Echo '....';
- Echo'
';
- }
- Else
- {
- Throw new exception ($ error, 12345 );
- }
- Echo 'the above throw exception, this line of code will not be executed, and then execute catch block
';
- }
- Catch (exception $ e)
- {
- Echo 'capture exception: '. $ e-> getmessage ()."
Error code: ". $ e-> getcode ().'
'; // Display $ error and 123456
- Echo'
';
- }
- Echo 'continue execution ';
3. getmessage (), a member function of the exception class ()
- $ File = './test/readme.txt ';
- Try
- {
- If (is_dir ($ file ))
- {
- Echo 'directory detected ';
- }
- Else
- {
- // Create an exception object. the error message will be returned by the member function getmessage () of the exception class.
- Throw new exception ('The directory or file not found ');
- }
- }
- Catch (exception $ e)
- {
- Echo 'capture exception: '. $ e-> getmessage ();
- Echo'
================================= ';
- Echo'
';
- }
- Echo 'Program execution completed ';
4. getfile (), a member function of the exception class ()
- $ File = './test/readme.txt ';
- Try
- {
- If (is_dir ($ file ))
- {
- Echo 'directory detected ';
- }
- Else
- {
- // Create an exception object. the error message will be returned by the member function getmessage () of the exception class.
- Throw new exception ('The directory or file not found ');
- }
- }
- Catch (exception $ e)
- {
- Echo 'capture exception: '. $ e-> getmessage ();
- Echo'
';
- Echo 'file where the error is located: '. $ e-> getfile ();
- Echo'
================================= ';
- Echo'
';
- }
- Echo 'Program execution completed ';
5. getline (), a member function of the exception class ()
- $ File = './test/readme.txt ';
- Try
- {
- If (is_dir ($ file ))
- {
- Echo 'directory detected ';
- }
- Else
- {
- // Create an exception object. the error message will be returned by the member function getmessage () of the exception class.
- Throw new exception ('The directory or file not found ');
- }
- }
- Catch (exception $ e)
- {
- Echo 'capture exception: '. $ e-> getmessage ();
- Echo'
';
- Echo 'file where the error is located: '. $ e-> getfile ();
- Echo'
';
- Echo 'error row: '. $ e-> getline ();
- Echo'
================================= ';
- Echo'
';
- }
- Echo 'Program execution completed ';
6. display warning or error messages
- Error_reporting (e_warning | e_error); // displays warnings and error messages.
- Echo $ uvar; // note information is generated but not displayed.
- Callfunc (); // a fatal error is generated and displayed.
- // 7. Custom error handling functions.
- Set_error_handler ('myhandler'); // custom error handling function
- Function myhandler ($ code, $ msg, $ file, $ line)
- {
- Echo"
";
- Echo "program$ FileDuring execution$ LineLine to generate an error. ";
- Echo"
";
- Echo "error code:$ CodeThe cause of the error is:$ Msg";
- }
- Echo $ uvar;