1. try catch finally 执行顺序问题
例如:
class Test {
public static void main( String args[] ) {
System.out.println( Test.test() );
}
public static String test() {
try {
System.out.println( "try" );
throw new Exception();
}
catch ( Exception e ) {
System.out.println( "catch" );
return "return";
}
finally {
System.out.println( "finally" );
}
}
}
执行结果是:
try
catch
finally
return
2. 即使try块中没有发生异常,也就是说catch块没有被执行,finally块也会被执行
例子:
class Test {
public static void main( String args[] ) {
System.out.println( Test.test() );
}
public static String test() {
try {
System.out.println( "try" );
}
catch ( Exception e ) {
System.out.println( "catch" );
return "return";
}
finally {
System.out.println( "finally" );
return "return in finally";
}
}
}
执行结果是:
try
finally
return in finally