PHP error handling and exception handling methods and examples tutorial (_php) Tutorial

Source: Internet
Author: User
Tags getmessage php error
PHP error handling and exception handling methods and examples tutorial in program development, error handling this piece is very important, today this article to tell him about in PHP development, error handling functions and examples of the importance of error handling.

PHP Tutorial error handling and exception handling methods and examples tutorial
In program development, error handling this piece is very important, today this article to tell him about in PHP development, error handling function and illustrate the importance of error handling.

1, built-in exception handling class.
2. Examples of catching and handling exceptions.
3. The member function of the exception class is GetMessage ().
4. The member function of the exception class is GetFile ().
5. The member function of the exception class is Getline ().
6. Display a warning or error message.
7. Custom error handling functions.

*/

1, built-in exception handling class.

Class exception
{
protected $message = ' Unknown exception '; Exception information
protected $code = 0; User-defined exception codes
protected $file; The file name of the exception that occurred
protected $line; The line number of the code where the exception occurred

function __construct ($message = null, $code = 0);

Final function getmessage (); Return exception information
Final function GetCode (); Return exception code
Final function getfile (); Returns the file name of the exception that occurred
Final function getline (); Returns the line number of the code where the exception occurred
Final function gettrace (); BackTrace () array
Final function gettraceasstring (); Gettrace () information that has been rasterized into a string

function __tostring (); A string that can be output
}

2. Examples of catching and handling exceptions.

Try
{
$error = ' throws exception information and jumps out of try Block
';
if (Is_dir ('./test '))
{
Echo ' detected: /CH16 is a directory ';
Echo '
';
Echo ' may continue to do some other operations ';
Echo '
';
echo ' ... ';
Echo '
';
}
Else
{
throw new Exception ($error, 12345);
}
Echo ' Above throw exception, this line of code will not execute, instead execute catch block
';
}
catch (Exception $e)
{
Echo ' catch exception: '. $e->getmessage (). "
Error code: ". $e->getcode (). '
'; Show $error and 123456
Echo '
';
}

Echo ' continues to execute ';

1 2

http://www.bkjia.com/PHPjc/632056.html www.bkjia.com true http://www.bkjia.com/PHPjc/632056.html techarticle PHP error handling and exception handling methods and examples tutorial in program development, error handling this piece is very important, today this article to tell him about in PHP development, error handling letter ...

  • Related Article

    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.