Custom php error handling and php exception handling

Source: Internet
Author: User
Tags php exception handling
To handle php exceptions, you must first know the php error level. let's take a look at the following: the value constant describes the 2E_WARNING non-fatal run-time error. Do not pause script execution. 8E_NOTICE

To handle php exceptions, you must first know the php error level, and look at the following:


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)


In general, we use if... else ..., when exit () and die () are used to handle errors, there are many limitations. how can we customize our processing mechanism?

1. Custom error functions


 

2. Custom error triggering mechanism


 80) {trigger_error ("error", E_USER_NOTICE) ;}?>

3. php exception handling:

(1) When an exception is thrown, the subsequent code will not be executed, but will jump directly to catch and directly execute the content in catch. if The exception is not caught, if set_exception_handler () is not used, a fatal error will occur.

Let's look at an example in the manual:


 1) {throw new Exception ("Value must be 1 or below");} return true;} // trigger an Exception in the "try" code block try {checkNum (2 ); echo 'If you see this, the number is 1 or below';} catch (Exception $ e) {echo 'message :'. $ e-> getMessage () ;}?>

Example of using set_exception_handler:


 ". $ E-> getMessage () ;}// modify the default exception processor set_exception_handler ("myException"); function func ($ num) {if ($ num> 100) {throw new Exception ("Your input value is too large! (Www.phpddt.com) ");} else {echo" OK ";} func (120);?>

Example of creating a custom Exception class:


 GetLine (). 'in'. $ this-> getFile ().':'. $ This-> getMessage ().''; Return $ errorMsg; }}$ num = 120; try {if ($ num> 100) {throw new myException ("The number is too large ");}} catch (myException $ e) {echo $ e-> errorMessage () ;}?>

Of course, multiple exceptions can also be captured:


 GetLine (). 'in'. $ this-> getFile ().':'. $ This-> getMessage ().''; Return $ errorMsg; }}$ num = 120; try {if ($ num> 100) {throw new myException ("The number is too large ");}} catch (myException $ e) {echo $ e-> errorMessage ();} catch (Exception $ e) {echo $ e-> getMessage () ;}?>

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.