Finally usage in Java __java

Source: Internet
Author: User

 Pen Test
Java processing exception using try-catch-finally statement capture processing exception, finally used to deal with a number of follow-up work, today encountered a very easy to wrong knowledge points, I think it is necessary to write something to make a summary.
To mention the use of the return statement, returns, regardless of the C language or in C + +, is the statement returning the value of the function. We all know the general effect of the return statement:
(1) Return a value to the method;
(2) End the current method.
And when there are multiple returns in a method, the first return is performed in the order in which they are executed, and of course the final function returned value is the first value to be executed. Because the method ends when the first return statement is executed.
But take a look at one of the Java code below, and the reader can anticipate the results:
 

public class Test {public
    static void Main (string[] args) {
        System.out.println (test (NULL) + "," + Test ("0") + "," + Test (""));

    public static int test (String str) {
        try {return
            Str.charat (0)-' 0 ';
        } catch (NullPointerException E1) {
  
   return 1;
        } catch (stringindexoutofboundsexception E2) {return

            2;
        } catch (Exception E3) {return

            3;
        } finally { return
            4;
        }

    }
  

The answer is: 4,4,4
Do not know if you have done right, anyway, I was wrong, ashamed.
attached below is the result of the execution (the result of the output method that added the exception information for easy Viewing):

Java.lang.NullPointerException at
    cn.test.example.Test.test (test.java:14) at
    Cn.test.example.Test.main ( Test.java:9)
java.lang.StringIndexOutOfBoundsException:String index out of range:0
    at Java.lang.String.charAt (string.java:646) at
    cn.test.example.Test.test (test.java:14)
    at Cn.test.example.Test.main (test.java:9)
4,4,4

Output Result Analysis:
In Java, finally, it must first be used in the last position of all the catch, and secondly he must execute it unconditionally, even if there is already an exception in the previous Try-catch statement, it will still execute. The most important thing to understand is that when calling tets ("null"), there is a nullpointerexception type of exception, and then return 1 is executed; by common sense, the return to this method will end, but see if the result is not. Although the return statement was run before the finally statement, but because finally usage is special, it will continue to execute the code in the last finally block before undoing the return statement, so the final result is 4,4,4.
It's a small problem, but it's easier to make mistakes, so here's a description.

Reprint please explain source: http://blog.csdn.net/yyg_2015/article/details/52188066

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.