-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFinalExample.java
55 lines (47 loc) · 1.25 KB
/
FinalExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.text.MessageFormat;
public class FinalExample {
public static void main(String[] args){
System.out.println(MessageFormat.format("return from try >>> {0} <<< " , getResult(20)));
// >>> 3.5 <<<
System.out.println(MessageFormat.format("return after try-finally block >>> {0} <<< " , getResultAndChangeIt(20)));
// >>> 3.5 <<<
System.out.println(MessageFormat.format("direct return from finally <<< {0} >>>", returnResult()));
}
private static float returnResult(){
try{
return 0;
}finally{
return -5;
}
}
private static float getResult(int argument){
float returnValue=0;
float magicNumber=70;
try{
returnValue=magicNumber/argument;
// V - place of execution finally block
return change(returnValue);
}finally{
returnValue=-5;
}
}
private static float getResultAndChangeIt(float argument){
float returnValue=0;
float magicNumber=70;
try{
if(argument<0){
throw new RuntimeException();
}
returnValue=magicNumber/argument;
// V - place of execution finally block
returnValue=change(returnValue);
}finally{
returnValue=-5;
}
return returnValue;
}
private static float change(float returnValue) {
System.out.println("update, before return");
return returnValue/1;
}
}