Php exception handling and php error handling mechanism

Source: Internet
Author: User
Tags php exception handling
Php error handling and php exception handling mechanism php error handling when we develop a program, sometimes the program has a problem, we can use the following methods to find out the error. & Nbsp; Development Phase: all error reports are output during development, which facilitates program debugging & nbsp; php error handling and php exception handling mechanism
Php error handling

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

Development Phase: all error reports are output during development, which facilitates program debugging.
Running stage: We do not allow the program to output any error report (not visible to users (including those who understand technology and do not understand technology ))
 
Write Error reports to logs
1. specify error report error_reporting = E_LL
2. disable error output display_errors = Off
3. enable the error log function log_errors = On

1. if no error log location is specified by default, the log is written to the WEB server by default.
2. specify a file name (writable) for the error_log option)
3. error_log = syslog

The following code example
 ";?>

Of course, php also provides the error_get_last () function to obtain the error message. function definition and usage: error_get_last () function to obtain the final error. This function returns the last error in the form of an array. The returned array contains four keys and values: [type]-error type [message]-error message [file]-file where an error occurs [line]-example of a small error:
 Output: Array ([type] => 8 [message] => Undefined variable: test [file] => D: \ www \ test. php [line] => 2)

PHP pre-defined variable $ php_errormsg is also provided after php5.4

$ Php_errormsg-previous error message

$ Php_errormsgThe variable contains the latest error message generated by PHP. This variable is only available in the scope where the error occurs, and the track_errors configuration item is enabled (disabled by default ).

Example:

 

Output:

Wrong parameter count for strpos()

So we are also very convenient... Is this helpful for debugging programs and troubleshooting errors?


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

Value Constant Description
2 E_WARNING Non-fatal run-time error. Do not pause script execution.
8 E_NOTICE

Run-time notification.

An error may occur when the script runs normally.

256 E_USER_ERROR Fatal user-generated error. This is similar to the E_ERROR set by the programmer using the PHP function trigger_error.
512 E_USER_WARNING Non-fatal user-generated warning. This is similar to the E_WARNING set by the programmer using the PHP function trigger_error.
1024 E_USER_NOTICE User-generated notifications. This is similar to the E_NOTICE set by the programmer using the PHP function trigger_error.
4096 E_RECOVERABLE_ERROR Possible fatal errors. Similar to E_ERROR, but can be captured by a user-defined handler. (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 definition:

Exception handling: Unexpected, unexpected, occurs during the running of the program. use an exception to change the normal process of the script.

Syntax format:
try{ //...}catch(Exception $e){ //...}

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

The code to be executed is placed in the TRY block. if an exception occurs in a statement during code execution, the program jumps directly to the CATCH block, and the error information and display are collected by $ e.

Try {} catch {} statement in PHP

To further handle exceptions, we need to use try {} catch {} in PHP to include Try statements and at least one catch statement. Any code that calls a method that may throw an exception should use the try statement. Catch statements are used to handle exceptions that may be thrown. Example:

I wrote a piece of code:

Define an exception class
Purpose: write one or more methods to solve the problem.
1. define the Exception class by yourself. it must be a subclass of Exception (built-in class). you can view the usage of Exception (built-in class) in the PHP Manual.
2. in the Exception class, only the constructor and toString () can be rewritten, and the rest are final.

 ";}Function open () {touch (" tmp.txt "); $ file = fopen (" tmp.txt "," r "); return $ file ;}}?>

1. if there is no problem with the code in try, it will be executed after the code in try is executed.
2. if an exception occurs in the code in try, an exception object (throw) is thrown and a parameter in catch is thrown, then, the code in try will not continue to execute and jump directly to catch to execute. the execution in catch is complete, and then continue to execute downward.
Note: the system prompts the exception. this is not the main task. we need to solve this exception in catch. if it cannot be solved, we will go out to the user in the following code, if I do not have this TMP. the TXT file will throw an exception.

If an exception exists, we can call the OPEN method to solve the exception.

 GetMessage ()."
"; // GetMessage () is a built-in method in PHP. you can directly call $ file = $ e-> open ();}

The following code is used to organize and handle multiple exceptions:

 ";}Function open () {touch (" tmp.txt "); $ file = fopen (" tmp.txt "," r "); return $ file ;}} class DemoException extends Exception {function pro () {echo "handles demo exceptions
";}} Class TestException extends Exception {function pro () {echo" handle test exceptions here
";}} Class HelloException extends Exception {} class MyClass {function openfile () {$ file = @ fopen (" tmp.txt "," r "); if (! $ File) throw new OpenFileException ("file opening 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 ()."
& Quot;} var_dump ($ file); echo & quot; 444444444444444444444
";

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.