Scope description of the variable in the Try-catch statement block __exception

Source: Internet
Author: User
Scope description of variables in try-catch statement block

The scope of a variable in a Try-catch statement block is the same as the scope of other statement blocks. Variables defined inside a statement block, scoped inside a statement block, and not externally visible.

    /* Statement block Internal   
    *
        /try{int a = 0;
    } catch (Exception e) {
        int b = 0;
    } finally{
        int c = 0;
    }
    /* Statement Block external * *       
    //a = 5;    Illegal, the compiler cannot recognize the variable
    //b = 5;    Illegal, the compiler cannot recognize the variable
    //c = 5;    Illegal, the compiler does not recognize the variable
A variable defined outside a statement block that can be modified within a statement block.
    /* Statement Block external *   
    /int a = 0;
    /* Statement block Internal   
    *
        /try{a = 5;
    } catch (Exception e) {
        a = 5;
    } finally{
        a = 5;
    }
    /* Statement Block external * *   
    System.out.println (a);

Run result is

5

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.