Exception handling-What is Exception in the catch (Exception $e) in PHP try catch?

Source: Internet
Author: User
Tags parse error php try catch
Title. is Exception the role of a functioning domain? Or the language structure?

Reply content:

First of all it is very clear that try ... catch ... is a language structure.
That being the case, it is not possible to understand the Exception in parentheses after the catch with the function parameter type qualification.

If there is no Exception, such as writing:

<?phptry {} catch ($e) {}

You will get an error:

Parse error:parse error, expecting "identifier (t_string)" ' or "namespace (t_namespace)" ' or ' "\ \ (t_ns_separator)" ' in. ..

Obviously, the Exception here is an "expectation" for judging the type of exception caught.

If you use a class scenario to translate, it's probably similar to the following:

<?php$e = new Exception;if ($e instanceof Exception) {    # do something ...}

If you are multiple catch, such as:

<?phptry {    throw new customexception ("Error processing Request", 1),} catch (Customexception $e) {    echo 1;} cat CH (Exception $e) {    echo 2;} Class Customexception extends exception{}

Although the exception we throw is Exception, the catch catches to the first stop, so here it only outputs 1, if the interchange is the catch (CustomException $e) catch (Exception $e) same.

This is also the case with the following:

<?php$e = new Customexception;if ($e instanceof customexception) {    echo 1;} elseif ($e instanceof Exception) {    echo 2;} Class Customexception extends exception{}

You can see that java,exception is a class name, and the catch's $e is an instance of the class.
You can also write a class to inherit the exception, and then catch the catch.

Type Bar. You can also write your own exception class.

The above is exception handling-what does Exception in the catch (Exception $e) in PHP try Catch do? For more information, please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.