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,否则会出现不可达代码.

解决方案 »

  1.   

    你把finally放catch外面
                    
      

  2.   

    不要在finally 里面使用 return 或者 new throw()之类的,否则会引起编译器警告 
      

  3.   

    你可以这样使其不报,不过不好。      @SuppressWarnings("finally")
    static boolean method()

    boolean b = false; 
       try  {  b = true; }
       catch(Exception e) 
       { 
      b =false; 
      throw new Exception(); 

       }
       finally  {return b;}
      
    }
      

  4.   


    static boolean method()
    {
       boolean b = false;
       try
       {
          some code here
          b = true;  }catch(Exception e)
      {
          b =false;
          throw new Exception();
            
      }finally
      {
         return b;
      }
    }这样,看的效果好点么?
      

  5.   

    不知这样行不行static boolean method() throws Exception { 
    boolean b = false; 
    try { 
    int i = 1/0; 
    System.out.println(i);
    b = true; 
    } catch(Exception e) { 
    b =false; 
    throw new Exception(); 
    }
    return b;
    }
      

  6.   

    1 楼的格式不清楚..请看最新的格式. 是在catch外面的.
      

  7.   

    如果finally上有return语句.   那么try/catch中的return语句将失效.    
      对于需要用return   返回一些数值的方法.   把return   语句放到finally将会出现非预期效果.   
      

  8.   

    这要怪楼主乱排版,其实 finally 本来就是放在 catch 外面的。问题是 finally 当中不能写 return,return 应该放在 finally 的外面,作为方法的最后一句。
      

  9.   

    int i = 1/0; 
    System.out.println(i);
    不好意思,把这两句换掉。
      

  10.   

    你可以考虑把b定义成类属性变量,然后你的方法只是改变它的值,而不返回值,这样你就不会有return的痛苦了。    static boolean b = false;

    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;
            }
        }
      

  11.   

    finally 内部使用 return 语句是一种很不好的习惯,如果try中还有return语句,它会覆盖了try 区域中 return 语句的返回值,程序的可读性差面对上述情况,其实更合理的做法是,既不在 try block 内部中使用 return 语句,也不在 finally 内部使用 return 语句,而应该在 finally 语句之后使用 return 来表示函数的结束和返回
      

  12.   


    申明成 boolean method()标志位 b申明在外面
    也成finally{
    //改变b的业务逻辑
    }return b;
      

  13.   

        我觉得原代码即使有Exception了也throw不了(只要return 在finally里)
      

  14.   

        因为如果它throw了,它就不能执行return 了(异常必定会终止某段代码),所以编译器为了执行
    return 而不会去执行throw(纯属个人意见)
                                
      

  15.   

    抛出异常,就不可能再有返回值,所以throw new Exception()和finally中的return是互斥的啊
    在你的这个逻辑中,实际上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;
        }