Exception handling-what is the difference between Exception and Exception in php?

Source: Internet
Author: User
An Exception needs to be caught in the code. The catch (Exception $ e) is invalid, but it can be changed to catch (Exception $ e. It probably depends on what the namespace is, but it is not very clear. In addition, it is found that all other exceptions used in this file are used (E... an exception needs to be caught in the code, use catch(Exception $e)Invalid, but changed catch(\Exception $e)You can. It probably depends on what the namespace is, but it is not very clear. In addition, it is found that all other exceptions used in this file are used. (Exception $e)Rather (\Exception $e)

Reply content:

Capture an exception in the code. usecatch(Exception $e)Invalid, but changedcatch(\Exception $e)You can. It probably depends on what the namespace is, but it is not very clear. In addition, it is found that all other exceptions used in this file are used.(Exception $e)Rather(\Exception $e)

If your php file defines a namespace, the class usage under the namespace must be added \
Indicates the root space, that is, the php class, not the class in your php file.

The cause that cannot be captured may cause the thrown exception to be mismatched. check whether the exception classes thrown by other exceptions pass through an inheritance relationship ..

Fans are right.

If your php file defines a namespace,catch(Exception $e)The Exception class under the namespace will be searched. if undefined, the class will not be found.

catch(\Exception $e)Indicates the root space. this class always exists.

Let's take a look.modern phpThis book.

Exception indicates the root namespace. to put it bluntly, it is the official Exception tired class of php. if it is not added, it may be the class under the namespace you currently define.

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.