Error handling and exception handling in PHP

Source: Internet
Author: User
This article mainly introduces the error handling and exception handling in PHP, the interested friend's reference, hope to be helpful to everybody.

Error handling:

1. Syntax errors
2. Run-time errors
3. Logic Error

Error Reporting:

Fatal error E_error
Warning E_warning
Note E_notice


Development phase: Output all error reports at development time, enabling us to debug

Run phase: Do not let the program output any kind of error reporting

To write error reports to the log

I. Specifying error Reporting Error_reporting=e_all (in Php.inn)

Two. Turn off error output Display_errors=off (in php.ini)

Three. Turn on the error log function Log_errors=on (in php.ini)

1. By default, if you do not specify an error log location, the log is written to the Web server by default

2. Specify a file name for the Error_log option, which is the error log

3. Write to the operating system log Error_log=syslog


<?php//note and warning will not terminate the program's operation, but the error will terminate the program's Run/* ERROR e_error warning e_warning Note e_notice*///All errors are output except note error_reporting (E_all & ~e_notice)///Set the value of the profile (temporary)//ini_set ("Upload_max_filesize", 2000000000);//Get the value of the configuration file//ini_get ("Upload_max_ FileSize ");//Turn off the display of error reports, generally use Ini_set (" Display_errors "," off ") in the run phase,//write error reports to the log ini_set (" Log_errors "," on ");// Log directory Ini_set ("Error_log", "D:/error.log"); GetType ($var);  Note GetType ();    Warning//getype ();    Error, the program terminates echo "############<br>";? >


exception Handling: Accidental, unexpected events that occur during program operation, using exceptions to change script normal process

A new and important feature in PHP5


try{

}catch (Exception object) {

}

1. If the code in the try does not have a problem, the execution of the code in try is done after the catch is executed

2. If the code in the try has an exception, throws an exception object (using throw), throws the parameter in the catch, the content after the exception code in the try is not executed, jumps directly into the catch to execute, the catch executes, and then continues to execute down


<?phptry{  echo "11111111<br>";  $file = @fopen ("./hello.txt", "R");  if (! $file) {    throw new Exception ("File open Failed");      }  echo "2222222222<br>";} catch (Exception $e) {  echo "3333333<br>";  echo $e->getmessage (). " <br> ";  Touch ("Hello.txt");  $file = @fopen ("./hello.txt", "R");} echo "4444444<br>";? >


The above is the whole content of this article, I hope that everyone's study has helped.


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.