Understanding the finally block

I wrote seven test cases to understand the behavior of a block finally. What is the logic of work finally?

package core;

public class Test {
    public static void main(String[] args) {
        new Test().testFinally();
    }

    public void testFinally() {
        System.out.println("One = " + tryOne());
        System.out.println("Two = " + tryTwo());
        System.out.println("Three = " + tryThree());
        System.out.println("Four = " + tryFour());
        System.out.println("Five = " + tryFive());
        System.out.println("Six = " + trySix());
        System.out.println("Seven = " + trySeven());
    }

    protected StringBuilder tryOne() {
        StringBuilder builder = new StringBuilder();
        try {
            builder.append("Cool");
            return builder.append("Return");
        }
        finally {
            builder = null;
        }
    }

    protected String tryTwo() {
        String builder = "Cool";
        try {
            return builder += "Return";
        }
        finally {
            builder = null;
        }
    }

    protected int tryThree() {
        int builder = 99;
        try {
            return builder += 1;
        }
        finally {
            builder = 0;
        }
    }

    protected StringBuilder tryFour() {
        StringBuilder builder = new StringBuilder();
        try {
            builder.append("Cool");
            return builder.append("Return");
        }
        finally {
            builder.append("+1");
        }
    }

    protected int tryFive() {
        int count = 0;
        try {
            count = 99;
        }
        finally {
            count++;
        }
        return count;
    }

    protected int trySix() {
        int count = 0;
        try {
            count = 99;
        }
        finally {
            count = 1;
        }
        return count;
    }

    protected int trySeven() {
        int count = 0;
        try {
            count = 99;
            return count;
        }
        finally {
            count++;
        }
    }
}

Why builder = nulldoesn’t work?

Why does it work builder.append("+1"), while count++(in trySeven ()) does not work?

+5
source share
7 answers

Once you make a return, the only way to override this is to make another return (as discussed in Returning from the finally block in Java , this is almost always a bad idea), or else abruptly terminate. Your tests never return from the final.

JLS § 14.1 . . try 1,2,3,4 7 - . § 14.20.2, try R, , finally .

finally ( , ), " try R.". , , , ; . , " try S ( R )". (S ).

, tryOne, :

finally {
            builder = null;
            return builder;
        }

S R.

builder.append("+1") tryFour, , StringBuilder , , try. .

tryFive trySix . , , , , , try-finally.

+11

, - , , .

public void deleteRows(Connection conn) throws SQLException {
    Statement statement = conn.createStatement();
    try {
        statement.execute("DELETE * FROM foo");
    } finally {
        statement.close();
    }
}

, , . , .

try {...} finally {...} , - . . , - :

public String thisShouldBeRefactored(List<String> foo) {
    try { 
        if(foo == null) {
            return null;
        } else if(foo.length == 1) {
            return foo.get(0);
        } else {
            return foo.get(1);
        }
    } finally {
        System.out.println("Exiting function!");
    }
}

. . , , . :

public String thisShouldBeRefactored(List<String> foo) {
    final String result;

    if(foo == null) {
        result = null;
    } else if(foo.length == 1) {
        result = foo.get(0);
    } else {
        result = foo.get(1);
    }

    System.out.println("Exiting function!");

    return result;
}
+2

finally , try. "return" : , - . , try , finally , .

:

  • finally blocks

():

int count = 1;//Assign local primitive count to 1
try{
  return count; //Assign primitive return value to count (1)
}finally{
  count++ //Updates count but not return value
}

():

StringBuilder sb = new StringBuilder();//Assign sb a new StringBuilder
try{
    return sb;//return a reference to StringBuilder
}finally{
    sb.append("hello");//modifies the returned StringBuilder
}

():

   StringBuilder sb = new StringBuilder();//Assign sb a new StringBuilder
   try{
      return sb;//return a reference to StringBuilder
   }finally{
      sb = null;//Update local reference sb not return value
   }

():

   int count = 1;   //assign count
   try{
      return count; //return current value of count (1)
   }finally{
      count++;      //update count to two but not return value
      return count; //return current value of count (2) 
                    //replaces old return value and exits the finally block
   }
+2

builder = null builder.append("+1") . , . , return, , .

, builder . builder=null builder. builder.append("+1") , .

0

builder = null ?
null, . , , .
builder.append("+1") work?
, .
count++ testFive()?
. 100, .

0

, return, , tryOne(): builder . , , , finally . , - :

protected StringBuilder tryOne() {
    StringBuilder builder = new StringBuilder();
    try {
        builder.append("Cool");
        builder.append("Return");
        StringBuilder temp = builder;
        return temp;
    } finally {
        builder = null;
    }
}

, , ( , ), :

protected StringBuilder tryOne() {
    StringBuilder builder = new StringBuilder();
    builder.append("Cool");
    builder.append("Return");
    StringBuilder temp = builder;
    builder = null;
    return temp;
}

builder = null , . builder.append("something") , temp, builder () .

, trySeven(), :

protected int trySeven() {
    int count = 0;
    count = 99;
    int temp = count;
    count++;
    return temp;
}

, int, , .

All that has been said remains a fact: including return statements in the try-finally block is pretty confusing, so if you have any choice in this matter, you'd better rewrite things so that all your return statements are outside of try blocks -finally.

0
source

StringBuilder cannot be null; it expects a string value.

Zero arguments usually do not work well with strings. count ++ not declared ??? builder.append ("") you add a line - good. count = count ++;

-3
source

All Articles