static boolean method()
{
boolean b = false;
try
{
some code here
b = true; }catch(Exception e)
{
b =false;
throw new Exception();
}finally
{
return b;
}
}
各位,想想办法吧..我的catch需要再throw.所以catch里不能带return,否则会出现不可达代码.
解决方案 »
- wait方法的问题
- java.util.Properties propertyNames方法
- 怎么改变jvm的内存?
- 关于SWT的线程问题,急!!!请大家帮帮我呀!
- 要调用另一个目录下的包中的类,怎么办?
- jBuilder字体的问题!
- JB5.0以上的版本在编译时,如何将检测所有相关性的class的特性取消掉?
- java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
- 关于JAVA和C/C++通过socket发送和接受数据的问题
- 请问String.equals()和String.equalsIgnoreCase()有什么区别啊?
- 关于socket连接的问题 (在线等)
- 100分!!!谁能告诉我这个方法中每步都是什么意思啊?
static boolean method()
{
boolean b = false;
try { b = true; }
catch(Exception e)
{
b =false;
throw new Exception();
}
finally {return b;}
}
static boolean method()
{
boolean b = false;
try
{
some code here
b = true; }catch(Exception e)
{
b =false;
throw new Exception();
}finally
{
return b;
}
}这样,看的效果好点么?
boolean b = false;
try {
int i = 1/0;
System.out.println(i);
b = true;
} catch(Exception e) {
b =false;
throw new Exception();
}
return b;
}
对于需要用return 返回一些数值的方法. 把return 语句放到finally将会出现非预期效果.
System.out.println(i);
不好意思,把这两句换掉。
static void method() throws Exception
{
try{
//some code here
b = true;
}catch(Exception e){
b =false;
throw new Exception();
}finally{
b = true; //or b = false;
}
}
申明成 boolean method()标志位 b申明在外面
也成finally{
//改变b的业务逻辑
}return b;
return 而不会去执行throw(纯属个人意见)
在你的这个逻辑中,实际上finally没有价值。而且,在这个程序中,如果程序抛出异常,则和b=false是等价的,你可以直接在外边捕捉异常,如果捕捉到异常,则必定意味着返回值为false.一般finally都是用来处理一些资源的释放和关闭,或者执行那些无论在何种条件下都必须执行的代码。
所以,建议改成:
static boolean method(){
boolean b = false;
try{
some code here
b = true; }catch(Exception e){
b =false;
throw new Exception();
}
return b;
}