Php error handling and exception handling methods and example tutorial (1/2) _ PHP Tutorial

Source: Internet
Author: User
Php error handling and exception handling methods and example tutorial (12 ). Php error handling and exception handling methods and instance tutorials in program development, error handling is very important. today this article will tell him about php development, error handling function php error handling and exception handling methods and instance tutorials in program development, error handling is very important. today this article will tell him about php development, the error handling function also provides examples to illustrate the importance of error handling.

Php Tutorial error handling and exception handling methods and 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. example of capturing and handling exceptions.
3. getmessage (), a member function of the exception class ().
4. getfile (), a member function of the exception class ().
5. getline (), a member function of the exception class ().
6. a warning or error message is displayed.
7. Custom error handling functions.

*/

// 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 ';

1 2

Http://www.bkjia.com/PHPjc/632056.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632056.htmlTechArticlephp error handling and exception handling methods and instance tutorials in program development, error handling is very important, today this article will tell him about in php Development, error handling letter...

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.