public class TryCatchFinallyTrick {
public static void main(String[] args) {
System.out.println("The output is: " + getName());
}
static int getName(){
int a = 3;
try{
System.out.println("I am try");
a = 4;
return a;
} catch(Exception e){
System.out.println("I am catch");
a = 5;
} finally {
System.out.println("I am finally.");
a = 6;
}
}
}
6
이라 생각할 것이다.[실행결과]
I am try
I am finally.
The output is:4
public class TryCatchFinallyTrick2 {
public static void main(String[] args) {
System.out.println("The output is: " + getName());
}
static String getName(){
String name = "a";
try{
System.out.println("I am try");
name = "try";
return name;
} catch(Exception e){
System.out.println("I am catch");
name = "catch";
} finally {
System.out.println("I am finally.");
name = "finally";
return name;
}
}
}
[실행결과]
I am try
I am finally.
The output is:finally