62,636
社区成员




int i = 1;
try
{
i = 2;
return i;
}
finally
{
i = 3;
System.out.println("finally block executed");
}
//执行结果是
//finally block executed
//2
//间接证明了finally在return的前面执行
int i = 1;
try
{
i = 2;
return i;
}catch(Exception e){
}finally
{
i = 3;
System.out.println("finally block executed");
return i;
}
//不但编译通不过了而且结果完全不同
//结果是
//finally block executed
//3
//这样的话finally是在return后面执行
try {
try {
// do something
} catch(Exception e) {
// deal the problem
}
} finally {
// clean the end
}
try {return true;} finally {return false}
import java.io.IOException;
public class Arcane1 {
public static void main(String[] args) {
try {
System.out.println("Hello world");
} catch(IOException e) {
System.out.println("I've never seen
println fail!");
}
}
}
public class Arcane2 {
public static void main(String[] args) {
try {
// If you have nothing nice to say, say nothing
} catch(Exception e) {
System.out.println("This can't
happen");
}
}
}
interface Type1 {
void f() throws CloneNotSupportedException;
}
interface Type2 {
void f() throws InterruptedException;
}
interface Type3 extends Type1, Type2 {
}
public class Arcane3 implements Type3 {
public void f() {
System.out.println("Hello world");
}
public static void main(String[] args) {
Type3 t3 = new Arcane3();
t3.f();
}
}
int i = 1;
try
{
i = 2;
return i;
} catch(Exception e) {
return i;
}
finally
{
i = 1000;
System.out.println("finally block executed");
}
try {
try {
............
}
catch(){
}
}
finally{
}