PHP exception handling technology, top exception processor _php tips

Source: Internet
Author: User
Tags error handling php exception handling throw exception
The function used to define the top-level exception handler is
Set_exception_handler ("My_exception");
The my_expection here is the developer's custom exception handler, which is the top exception processor, and only if there is no function in the program to handle the exception, there is a top-level exception handler to handle the exception, and if no top-level exception handler is defined, then the system default exception handler handles the exception

An example is provided:
Copy Code code as follows:

<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/><?php
Set_exception_handler ("My_expection");
function My_expection () {
echo "Here is the top exception handler";
}
try{
Nohello ("Hello");
}catch (Exception $e) {
Throw $e;
}
function Nohello ($nohello) {
if ($nohello = = "Hello") {
throw new Exception ("Can't enter Hello");
}else{
echo "Input Success";
}
}
?>

A problem with PHP exception handling
Copy Code code as follows:

<?php
try {
$a =10/0;
}catch (Exception $e) {
echo "Throws an exception";
}
?>

The above code, does not output "throw exception", but output error message: Warning:division by zero in ...

Description: Instead of throwing an exception in the try block, it is handled according to the system default error handling mechanism

So it's possible to catch an exception, depending on whether there's really a throw exception

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.