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. 1. built-in exception handling class, 2. Examples of capturing and handling exceptions, 3. exception class... 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

 

2. example of capturing and handling exceptions

 '; If (is_dir ('./test') {echo 'detects that ../ch16 is a directory'; echo'
'; Echo' other operations may be continued '; echo'
'; Echo'... '; echo'
';} Else {throw new exception ($ error, 12345);} echo' if the above throw exception occurs, this line of code will not be executed and the catch block will be executed in turn
';} Catch (exception $ e) {echo' catch an exception: '. $ e-> getmessage ()."
Error code: ". $ e-> getcode ().'
'; // Display $ error and 123456 echo'
';} Echo 'continue to execute';?>

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

 Getmessage (); echo'
================================='; Echo'
';} Echo' program execution completed ';?>

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

 Getmessage (); echo'

'; Echo' error File: '. $ e-> getfile (); echo'
================================='; Echo'
';} Echo' program execution completed ';?>

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

 Getmessage (); echo'

'; Echo' error File: '. $ e-> getfile (); echo'

'; Echo' error row: '. $ e-> getline (); echo'
================================='; Echo'
';} Echo' program execution completed ';?>

6. display warning or error messages

 

7. Custom error handling functions.

 "; Echo" program$ FileDuring execution$ LineLine to generate an error. "; Echo"
"; Echo" error code:$ CodeThe cause of the error is:$ Msg";} Echo $ uvar;?>


Link to this article:

Add to favorites ^ please keep the tutorial address.

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.