PHP error Handling and PHP exception handling mechanism _php tutorial

Source: Internet
Author: User
Tags php error php exception handling syslog

PHP Error Handling

When we develop the program, sometimes there is a problem with the program, we can use the following methods to find out the error.

Development phase: Output all error reports at development time, which will help us to debug the program
Run stage: We do not let the program output any kind of error report (not to be seen by the user (including those who know the technology, who do not know the technology))

To write error reports to the log
I. Specifying error Reporting error_reporting = E_ll
Second, off error output Display_errors = Off
Third, turn On error log function log_errors = On

1. Default if the error log location is not specified, the default write in the Web server's log
2. Specify a file name (writable) for the Error_log option
3. Write to the operating system log Error_log=syslog

The following code example

 
  ";?>

Of course PHP also provides the function error_get_last () to get the error message

function definition and usage

The Error_get_last () function gets the last error that occurred. The function returns the last error in the form of an array. The returned array contains 4 keys and values: [Type]-error type [message]-error message [file]-where the error occurred [line]-where the error occurred

Small example:

 
  Output: Array ([type] + 8 [message] = = Undefined Variable:test [File] = D:\www\test.php [line] + 2)
So we are also very convenient ... Is it helpful to debug programs and troubleshoot errors?

These error reporting levels are different types of errors that the error handler is designed to handle:

value Constants Description
2 E_warning A non-fatal run-time error. Script execution is not paused.
8 E_notice

Run-time notice.

The script discovers that an error may have occurred, but it may also occur when the script is running correctly.

256 E_user_error A fatal user-generated error. This is similar to the e_error that programmers use to set the PHP function Trigger_error ().
512 E_user_warning A non-fatal user-generated warning. This is similar to the e_warning that programmers use to set the PHP function Trigger_error ().
1024 E_user_notice User-generated notifications. This is similar to the e_notice that programmers use to set the PHP function Trigger_error ().
4096 E_recoverable_error A fatal error that can be caught. Similar to E_error, but can be captured by user-defined handlers. (see Set_error_handler ())
8191 E_all

All errors and warnings, except level e_strict.

(In PHP 6.0,e_strict is part of E_all)

PHP exception handling mechanism

Defined:

Exception handling: Accidental, is unexpected in the process of running the program, using exceptions to change the script normal process

Syntax format:

try{//...} catch (Exception $e) {//...}

try{}catch{} in PHP is exception handling.

The code that will be executed is put into a try block, and if an exception occurs during the execution of the code, the program jumps directly into the catch block and the $e collects the error information and displays it.

try{}catch{} statements in PHP

To further handle the exception, we need to use PHP try{}catch{}----include a try statement and at least one catch statement. Any code that calls a method that might throw an exception should use a try statement. The catch statement is used to handle the exceptions that may be thrown.

Example:

I write a piece of code:

Define an exception class yourself
Function: Write one or more methods to resolve when this exception occurs
1. You define the exception class, must be exception (built-in class) subclass, you can see the PHP manual inside the exception (built-in class) use method
2. Only the constructor method and ToString () in the exception class can be overridden, and the others are final

 
  ";    }    function open () {        Touch ("Tmp.txt");        $file =fopen ("Tmp.txt", "R");        return $file;    }}? >

1. If the code in try does not have a problem, the code in try is executed after the catch is executed
2. If the code in try has an exception, throws an exception object (using throw), throws the parameter in the catch, then the code in the try is no longer going to go directly to the catch to execute, the catch executes, and then continues to execute
NOTE: Hint what happened, this is not the main thing we want to do, need to solve this exception in catch, if not solve, then go out to the user in the following code, if I do not have this TMP.TXT file, it will throw an exception.

If there is an exception, we can call the Open method to resolve the exception.

 
  GetMessage (). "
"; GetMessage () is a built-in method within PHP that can be called directly $file = $e->open (); }

The following organizes the code and several exception handling methods:

 ";} function open () {Touch ("Tmp.txt"), $file =fopen ("Tmp.txt", "R"), Return $file;}} Class Demoexception extends Exception {function Pro () {echo] handles exceptions that occur with demo
";}} Class TestException extends Exception {function Pro () {echo "handles the exception that occurred with test
";}} Class Helloexception extends Exception {}class MyClass {function OpenFile () {$file = @fopen ("Tmp.txt", "R"); if (! $file) throw new Openfileexception ("File open Failed");} Function demo ($num =0) {if ($num ==1) throw new Demoexception ("demo exception");} function test ($num =0) {if ($num ==1) throw new TestException ("Test Error");} function Fun ($num =0) {if ($num ==1) throw new Helloexception ("###########");}} Try{echo "11111111111111
"; $my =new MyClass (); $my->openfile (); $my->demo (0); $my->test (0); $my->fun (1); echo" 22222222222222222
";} catch (Openfileexception $e) {//$e =new Exception (); Echo $e->getmessage (). "
"; $file = $e->open ();} catch (Demoexception $e) {echo $e->getmessage (). "
"; $e->pro ();} catch (TestException $e) {echo $e->getmessage (). "
"; $e->pro ();} catch (Exception $e) {echo $e->getmessage (). "
";} Var_dump ($file); echo "444444444444444444444
";

http://www.bkjia.com/PHPjc/742411.html www.bkjia.com true http://www.bkjia.com/PHPjc/742411.html techarticle PHP Error Handling when we develop the program, sometimes there are problems with the program, we can use the following methods to find out the error. Development phase: Output all error reports during development, ...

  • 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.