Try-catch-PHP Exception Handling, 1 divided by 0

Source: Internet
Author: User
Tags php exception handling
Try {code...} catch (Exception $ e) {code...} No content in the log after execution. Why? What types of errors can trycatch capture? Try {

$a = 1/0;

} Catch (Exception $ e ){

file_put_contents("filelog.log", $e.getMessage(), FILE_APPEND);

}

No log content after execution. Why? What types of errors can be captured by try catch?

Reply content:

Try {

$a = 1/0;

} Catch (Exception $ e ){

file_put_contents("filelog.log", $e.getMessage(), FILE_APPEND);

}

No log content after execution. Why? What types of errors can be captured by try catch?

try {} catch {}YesPHP 5The later mechanism is added because the switchover from the original error mechanism still requires a transitional period.PHP 5These two error mechanisms coexist throughout the lifecycle, and most of the core errors remainErrorFormat. However, errors in most extensions have gradually changedThrowThrow andPHP 7There are also many core errorsThrowThrow, but completely switchesThrowBefore the mechanism, it is best to handle the coexistence of the two mechanisms.

Division by zero this is warning not Exception.

Use set_error_handler to capture

set_error_handler(function($errno, $errstr) {    var_dump($errstr);});$a = 1/0;

It is not recommended to capture such exceptions. You just need to consider it carefully when writing the program.

Determine whether the divisor is null and 0 before execution.

Mysql can also run 1/0! The result is null.

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.