Php error handling and exception handling methods and examples

Source: Internet
Author: User
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

  1. Class exception
  2. {
  3. Protected $ message = 'unknown exception'; // exception information
  4. Protected $ code = 0; // custom exception code
  5. Protected $ file; // file name with an exception
  6. Protected $ line; // The code line number with an exception
  7. Function _ construct ($ message = null, $ code = 0 );
  8. Final function getmessage (); // returns exception information
  9. Final function getcode (); // returns the exception code
  10. Final function getfile (); // returns the file name with an exception
  11. Final function getline (); // return the code line number in which an exception occurs.
  12. Final function gettrace (); // backtrace () array
  13. Final function gettraceasstring (); // gettrace () information formatted as a string
  14. Function _ tostring (); // output string
  15. }

2. example of capturing and handling exceptions

  1. Try
  2. {
  3. $ Error = 'throws exception information and jumps out of the try block.
    ';
  4. If (is_dir ('./test '))
  5. {
  6. Echo '../ch16 is a directory ';
  7. Echo'
    ';
  8. Echo 'other operations may be continued ';
  9. Echo'
    ';
  10. Echo '....';
  11. Echo'
    ';
  12. }
  13. Else
  14. {
  15. Throw new exception ($ error, 12345 );
  16. }
  17. Echo 'the above throw exception, this line of code will not be executed, and then execute catch block
    ';
  18. }
  19. Catch (exception $ e)
  20. {
  21. Echo 'capture exception: '. $ e-> getmessage ()."
    Error code: ". $ e-> getcode ().'
    '; // Display $ error and 123456
  22. Echo'
    ';
  23. }
  24. Echo 'continue execution ';

3. getmessage (), a member function of the exception class ()

  1. $ File = './test/readme.txt ';
  2. Try
  3. {
  4. If (is_dir ($ file ))
  5. {
  6. Echo 'directory detected ';
  7. }
  8. Else
  9. {
  10. // Create an exception object. the error message will be returned by the member function getmessage () of the exception class.
  11. Throw new exception ('The directory or file not found ');
  12. }
  13. }
  14. Catch (exception $ e)
  15. {
  16. Echo 'capture exception: '. $ e-> getmessage ();
  17. Echo'
    ================================= ';
  18. Echo'
    ';
  19. }
  20. Echo 'Program execution completed ';

4. getfile (), a member function of the exception class ()

  1. $ File = './test/readme.txt ';
  2. Try
  3. {
  4. If (is_dir ($ file ))
  5. {
  6. Echo 'directory detected ';
  7. }
  8. Else
  9. {
  10. // Create an exception object. the error message will be returned by the member function getmessage () of the exception class.
  11. Throw new exception ('The directory or file not found ');
  12. }
  13. }
  14. Catch (exception $ e)
  15. {
  16. Echo 'capture exception: '. $ e-> getmessage ();
  17. Echo'

    ';
  18. Echo 'file where the error is located: '. $ e-> getfile ();
  19. Echo'
    ================================= ';
  20. Echo'
    ';
  21. }
  22. Echo 'Program execution completed ';

5. getline (), a member function of the exception class ()

  1. $ File = './test/readme.txt ';
  2. Try
  3. {
  4. If (is_dir ($ file ))
  5. {
  6. Echo 'directory detected ';
  7. }
  8. Else
  9. {
  10. // Create an exception object. the error message will be returned by the member function getmessage () of the exception class.
  11. Throw new exception ('The directory or file not found ');
  12. }
  13. }
  14. Catch (exception $ e)
  15. {
  16. Echo 'capture exception: '. $ e-> getmessage ();
  17. Echo'

    ';
  18. Echo 'file where the error is located: '. $ e-> getfile ();
  19. Echo'

    ';
  20. Echo 'error row: '. $ e-> getline ();
  21. Echo'
    ================================= ';
  22. Echo'
    ';
  23. }
  24. Echo 'Program execution completed ';

6. display warning or error messages

  1. Error_reporting (e_warning | e_error); // displays warnings and error messages.
  2. Echo $ uvar; // note information is generated but not displayed.
  3. Callfunc (); // a fatal error is generated and displayed.
  4. // 7. Custom error handling functions.
  5. Set_error_handler ('myhandler'); // custom error handling function
  6. Function myhandler ($ code, $ msg, $ file, $ line)
  7. {
  8. Echo"
    ";
  9. Echo "program$ FileDuring execution$ LineLine to generate an error. ";
  10. Echo"
    ";
  11. Echo "error code:$ CodeThe cause of the error is:$ Msg";
  12. }
  13. Echo $ uvar;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.