PHP exception and error handling mechanism

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

Let's start with the concept of error and exception in PHP.

PHP Error: Is the PHP program itself, the problem is generally caused by illegal syntax, environmental problems, so that the compiler can not pass the check, or even can not run the situation. Usually encountered warming, notice are wrong, but the level is different.

PHP Exceptions: Generally, the business logic is not expected to occur, and the normal process is different from the situation, not a syntax error.

PHP exception handling mechanism for reference to Java C + +, but PHP exception handling mechanism is not sound. The purpose of the exception handling mechanism is to separate the code that the program executes properly from the code that is handling the exception.

PHP is unable to automatically catch exceptions (most of them), only actively throws exceptions and snaps. In other words, for exceptions, it is predictable.

Take the divisor of 0 as an example to see how PHP is handled

<?php $a = 0;try {echo 4/$a;} catch (Exception $e) {echo $e->getmessage ();} Error, PHP is unable to automatically catch the exception, you must manually throw?>

Manually thrown below

<?php $a = 0;try {if ($a = = 0) {throw new Exception (' divisor cannot be 0 ', 1);} Echo 4/$a;//Do not execute}catch (Exception $e) {echo $e->getmessage ();}? >

You can see that the catch catches the exception.

PHP in the use of the mechanism of exceptions, generally used for business logic judgment above, when the business logic is not normal, special processing.

Here's a look at the PHP error handling mechanism. PHP provides the Set_error_handler () function for error handling. When an error occurs, Set_error_handler will handle the error uniformly.

As shown below

<?php $a = 0;function handle ($errno, $errstr, $errfile, $errline) {echo $errfile;} Set_error_handler (' handle '); echo 4/$a; Echo php_eol;echo ' hehe ';? >

Above the

Echo 4/$a; An error occurred at this point in the error handling function handle, error handling, generally we add die in the handle, terminate execution.

PHP exception and error handling mechanism

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.