1. try-catch-finally 后面
private void scene01 ( ) {
try {
System . out. println ( "--try" ) ;
} catch ( Exception ex) {
System . out. println ( "--catch" ) ;
} finally {
System . out. println ( "--finally" ) ;
}
System . out. println ( "--after" ) ;
return ;
}
-- try
-- finally
-- after
2. try-catch-finally 后面、try 里面
不操作局部变量
private void scene02( ) {
try {
System. out. println( "--try" ) ;
return ;
} catch ( Exception ex) {
System. out. println( "--catch" ) ;
} finally {
System. out. println( "--finally" ) ;
}
System. out. println( "--after" ) ;
return ;
}
-- try
-- finally
结论:
程序执行 try 块中 return 之前代码。 再执行 finally 块,最后执行 try 中 return。 注意:
try-catch-finally 块之后的 return,因为程序在 try 中已经 return,所以不再执行。
操作局部变量
private String scene02_1( ) {
String result = "" ;
try {
result = "try" ;
return result;
} finally {
result = "finally" ;
}
return result;
}
try
结论:当 try 和 finally 同时操作某变量时,try 里有 return 但 finally 里没有 return 时,finally 的代码不会改变变量的值 。
3. try-catch-finally 后面、try 抛异常、catch 里面
不操作局部变量
private void scene03( ) {
try {
System. out. println( "--try" ) ;
if ( 1 < 2) {
throw new RuntimeException( ) ;
}
} catch ( Exception ex) {
System. out. println( "--catch" ) ;
return ;
} finally {
System. out. println( "--finally" ) ;
}
System. out. println( "--after" ) ;
return ;
}
-- try
-- catch
-- finally
结论:执行 catch 的 return 前执行 finally 中的内容。
操作局部变量
private String scene03_1 ( ) {
String result = "" ;
try {
if ( 1 < 2 ) {
throw new RuntimeException ( ) ;
}
} catch ( Exception ex) {
result = "catch" ;
return result;
} finally {
result = "finally" ;
}
return result;
}
catch
结论:当 catch 和 finally 同时操作某变量时,catch 里有 return 但 finally 里没有 return 时,finally 的代码不会改变变量的值。
4. try 里面、finally 里面
不操作局部变量
private void scene04 ( ) {
try {
System . out. println ( "--try" ) ;
return ;
} catch ( Exception ex) {
System . out. println ( "--catch" ) ;
} finally {
System . out. println ( "--finally" ) ;
return ;
}
}
--try
--finally
结论:
程序执行 try 块中 return 之前代码。 再执行 finally 块,因为 finally 块中有 return 所以提前退出。 注意:try 的 return 不再执行!出口是 finally 的 return。
操作局部变量
private String scene04_1 ( ) {
String result = "" ;
try {
result = "try" ;
return result;
} finally {
result = "finally" ;
return result;
}
}
finally
结论:当 try 和 finally 同时操作某变量时,try 里有 return 且 finally 里也有 return 时,finally 的代码会改变变量的值。
5. catch 里面、finally 里面
不操作局部变量
private void scene05 ( ) {
try {
if ( 1 < 2 ) {
throw new RuntimeException ( ) ;
}
System . out. println ( "--try" ) ;
} catch ( Exception ex) {
System . out. println ( "--catch" ) ;
return ;
} finally {
System . out. println ( "--finally" ) ;
return ;
}
}
--catch
--finally
结论:
程序执行 catch 块中 return 之前代码; 再执行 finally 块,因为finally块中有 return 所以提前退出。 注意:catch 的 return 不再执行!出口是 finally 的 return。
操作局部变量
private String scene05_1 ( ) {
String result = "" ;
try {
if ( 1 < 2 ) {
throw new RuntimeException ( ) ;
}
} catch ( Exception ex) {
result = "catch" ;
return result;
} finally {
result = "finally" ;
return result;
}
}
finally
结论:当 catch 和 finally 同时操作某变量时,catch 里有 return 且 finally 里也有 return 时,finally 的代码会改变变量的值。