PHP7 Error Handling

Source: Internet
Author: User
Tags throw exception

Recently learned the next PHP7 new features tutorial, recorded some study notes.

PHP 7 Changes the way most errors are reported. Unlike the traditional error reporting mechanism of PHP 5, most errors are now thrown as error exceptions.

This error exception can be captured as a normal exception by the Try/catch block. If there is no matching try/catch block, the exception handler is called (registered by Set_exception_handler ()) for processing. If the exception handler has not been registered, it is handled in the traditional way: it is reported as a fatal error (Fatal error).

The error class is not extended from the Exception class, so the code with catch (Exception $e) {...} does not catch the error. You can catch the error with try{} catch (Error $e) {...}, or by registering the exception handler (Set_exception_handler ()).

Error Exception Hierarchy

Errorarithmeticerrorassertionerrordivisionbyzeroerrorparseerrortypeerrorexception ...

Instance

<?PHPclassmathoperations {protected $n= 10; //find remainder operation, divisor is 0, throw exception        Public functionDooperation ():string        {           Try {              $value=$this->n% 0; return $value; } Catch(Divisionbyzeroerror$e) {              return $e-GetMessage (); }        }     }      $MATHOPERATIONSOBJ=Newmathoperations (); Print($MATHOPERATIONSOBJ-dooperation ()); ?>

The above program execution output results are:
Modulo by Zero

Reproduced from the technology to bring you closer to me, original address: http://coderschool.cn/1749.html

PHP7 Error Handling

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.