考考你Java小问题.

在Java中try-catch-finally用来异常管理. 如果只有try-finally能运行吗?



ex.



try

{

dosomething();

}

finally

{

docleaningup();

}

不行吧?如果那样就不需要try了,反正得不到exception



anyway没试过不敢断言

1.不行,try catch要配对

2.这样做好像没有什么意义啊

既然是异常,就要把异常的条件写出来,没CATCH就是没有条件了

但是不是异常的话,try和finally是可以使用的,但就不叫异常管理了



所以我认为可以运行,但是没意义




于 2003-04-27 22:34, root 写:

在Java中try-catch-finally用来异常管理. 如果只有try-finally能运行吗?



ex.



try

{

dosomething();

}

finally

{

docleaningup();

}




当然可以运行拉, 不过没有catch 到东西



也可以多重catch

try {

dosomething();

}

catch (Power e) {

System.out.println(“power”);

}

catch (Apple e) {

System.out.println(“apple”);

}



finally {

//总是运行, 跟try 的东西不关联, try退出, finally就会运行

System.out.println(“rox!”);

}











[ 编辑者 TAUREN 于日期 29Apr03 ]

有一种情况,就是希望把处理过程中的任何异常都抛到上一级处理,所以就不用catch.但是,又希望能够及时释放掉资源。所以必须在finally中做。

ha, i like it



but my coursework really pissed me off



finally is optional, if you use try you should use catch, try is the process to find some exception,if there is really some exceptions there you must catch it.