public void test() throws Exception{
  try{
    ...
  }catch(Exception e){
    throw e;
  }finally{
    ...
  }
}如下形式调用test()方法:
try{
  test();
}catch(Exception e){
  System.out.println(e.getMessage());
}
输出结果为空,如果将test方法中的finally块去掉就能输出错误信息,这是怎么回事啊?

解决方案 »

  1.   

    可能是finally块里面出异常吧。
      

  2.   

    finally总会执行,throw语句将结束方法的执行,但是指向完了throw语句之后,控制权就转到finally了,throw语句就起不到抛出异常结束方法的作用了
      

  3.   

    你在try或者catch加个return语句也有这样的问题
      

  4.   


    public static int test(){
            try{
                return 1;
            }
            catch(Exception e){
                
            }
            finally{
                return 2;
            }
        }这个更能说明问题.
      

  5.   

    这样相当于一个嵌套try.
    内层(test())try产生异常.内层catch已经捕获,外层
    catch(Exception e){System.out.println(e.getMessage());} 
    就不会再捕获执行了.
    去掉finally也不会捕获执行.
      

  6.   

    这个是因为你的try block中没有一场被抛出。手工加一个一场测试一下public void test() throws Exception{ 
      try{ 
        ... 
        throw new Exception("See me?");  }catch(Exception e){ 
        throw e; 
      }finally{ 
        ... 
      } 

      

  7.   

    try{ 
      test(); 
    }catch(Exception e){ 
      System.out.println(e.getMessage()); 

    这里执行到test()时执行下面的test函数
    public void test() throws Exception{ 
      try{ 
        ... 
      }catch(Exception e){ 
        throw e; 
      }finally{ 
        ... 
      } 

    而这个test函数在执行完finally里后程序就结束了,它不会返回到下面程序中的catch再执行,所以不会有输出,当你去掉了finally后它就会再执行完上面函数的方法后继续执行下面程序中的catch语句,就会返回e.getMseesage()的值了
    try{ 
      test(); 
    }catch(Exception e){ 
      System.out.println(e.getMessage()); 

      

  8.   

    昨天回复(6#)有点问题.(把它想成一个try多个catch了)
    貌似有2个 try 就要 catch 2次...去不去掉 finally 都没什么问题.public class Test
    {
    void test() throws Exception
    {
      try
      {
        int a = 1/0;
      }catch(Exception e){
        throw e;
      }
      finally
      {
        System.out.println("finally");
      }
    }

    public static void main(String[] args)
    {
    try
    {
    new Test().test();
    }
    catch (Exception e)
    {
    System.out.println("Do you see me ?");

    }
    }
    /*
    output:
    finally
    Do you see me ?
    */public class Test
    {
    void test() throws Exception
    {
      try
      {
        int a = 1/0;
      }catch(Exception e){
        throw e;
      }
      /*finally
      {
        System.out.println("finally");
      }*/
    }

    public static void main(String[] args)
    {
    try
    {
    new Test().test();
    }
    catch (Exception e)
    {
    System.out.println("Do you see me ?");

    }
    }
    //output:Do you see me ?
    如果没有把异常抛出,异常被test()的try吞了情况就不一样了.
    public class Test
    {
    void test() throws Exception
    {
      try
      {
        int a = 1/0;
      }catch(Exception e){
        //throw e;
      }
      /*finally
      {
        System.out.println("finally");
      }*/
    }

    public static void main(String[] args)
    {
    try
    {
    new Test().test();
    }
    catch (Exception e)
    {
    System.out.println("Do you see me ?");

    }
    }
    //output:(什么也没有...)