例子:
package
algorithms.com.guan.javajicu;
public
class
TestDemo
{
public
static
String output = ””;
public
static
void
foo(inti)
{
try
{
if
(i ==
1
)
{
throw
new
Exception();
}
}
catch
(Exception e)
{
output += “
2
”;
return
;
}
finally
{
output += “
3
”;
}
output += “
4
”;
}
public
static
void
main(String[] args)
{
foo(
0
);
foo(
1
);
System.out.println(output);
}
}
输出为3,4,2,3,当输入为0时没有异常,finally是必须执行的所以输出为3,4.当输入为1时,异常,输出2,3.因为输出为2时已经return,所以不输出4.