Go: Execution relationship of finally and return in Java

Source: Internet
Author: User
Tags finally block

Finally can be understood in two ways

1. Timing of implementation. Finally always executes (unless System.exit ()), normally executes after a try and throws an exception after Catche

2. The return value problem. You can assume that the return value of a return statement in a try (or catch) is placed at the top of the line stacks: If the return value is a base type, the top is the value, and if the return value is a reference type, the top holds the reference. The return statement in finally can modify the object corresponding to the reference and cannot modify the base type. However, both the base type and the reference type can be overridden by the concrete value of the finally return

3. It is not recommended to use the return statement in Finally, and if so, eclipse will warning "Finally block does not complete normally"

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 package com.ljn.base;/** * @author lijinnan * @date:2014-1-22 */publicclassFinallyTest {    publicstaticvoidmain(String[] args) {        System.out.println(test());        System.out.println(testPrimitive());    }    publicstaticStudent test() {        Student result = new Student("Tom", 0);        try{            result.setAge(1);            returnresult;        catch(Exception e) {            result.setAge(2);            returnresult;        finally{            result.setAge(3);       //引用类型的返回值,可被修改            //return new Student("Kobe", 33);   //可以被“具体值”覆盖        }            }        public staticinttestPrimitive() {        intx = 0;        try{            x = 1;            returnx;        catch(Exception e) {            x = 2;            returnx;        finally{            x = 3;          //基本类型的返回值,不可被修改            //return x;    //可以被“具体值”覆盖        }            }        privatestaticclassStudent {        privateString name;        privateint age;                publicStudent(String name, intage) {            this.name = name;            this.age = age;        }        @Override        publicString toString() {            return"Student [name="+ name + ", age=" + age + "]";        }        publicvoidsetAge(intage) {            this.age = age;        }            }}

Go: Execution relationship of finally and return in Java

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.