Exception handling-What is the difference between exception and exception in PHP?

Source: Internet
Author: User
You need to catch an exception in your code, using the catch(Exception $e)Invalid, but changed to catch(\Exception $e)We can. Probably know what the namespace is about, but the specific is not very clear, ask for answers. In addition, it is used to find other exceptions in this file. (Exception $e)Rather than (\Exception $e)

Reply content:

You need to catch an exception in your code that is catch(Exception $e) invalid, but catch(\Exception $e) you can change it. Probably know what the namespace is about, but the specific is not very clear, ask for answers. In addition, it is found that other exceptions in this file are used (Exception $e) rather than(\Exception $e)

If your PHP file defines a namespace, then the class usage below the namespace must add \
Represents the root space, which is PHP's own class, not the class in your PHP file

The reason for the inability to catch may be that because the thrown exception does not match, check that exception classes thrown by other exceptions are passed through a layer of inheritance:

The fans are right about the situation.

If your PHP file defines a namespace, it catch(Exception $e) looks for the exception class under that namespace, and if undefined, there is a problem with the class not found.

catch(\Exception $e)Represents the root space, and this class is always present.

It is recommended to take a look at modern php this book.

Exception represents the root namespace, which is the official PHP exception class, not add the words may be your current definition of the class under the namespace

  • 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.