分享

try/catch/finally测试

 posondlq 2012-09-12

try/catch/finally测试  

结论:
1、如果某个异常发生的时候没有在任何地方捕获它,程序就会终止执行,并在控制台上显示异常信息
2、不管是否有异常被捕获,finally子句中的代码都会执行
3、当代码抛出一个异常时,就会终止对方法中剩余代码的处理,并退出这个方法
4、finally后面的代码被执行的条件:
    a、代码没有抛出异常
    或者b、代码抛出异常被捕获,而且捕获的时候没有再抛出异常
5、建议用try/catch和try/finally语句块,前者是将异常拦截;而后者是为了将异常送出去,并执行所需代码
6、finally优先于try中的return语句执行,所以finally中,如果有return,则会直接返回,而不是调用try中的return;catch中的return也是一样,也就是说,在return之前,肯定会先调用finally中的return

例1:
// 测试捕获异常后,再次抛出异常,不会执行finally后面的语句// 1
// 因为编译就通不过,直接报:unreacchable statement
private static String testTryCatch() throws Exception
{
  try
  {
    throw new Exception();
  }
  catch (Exception e)
  {
    // 捕获异常后再次抛出异常    
    throw new Exception();
  }
  finally
  {
    System.out.println("finally");  
  }
  return "after finally";  // 1
}

例2:
// 测试异常被捕获,会继续执行finally后面的语句// 1
private static void testTryCatch()
{
  try
  {
    throw new Exception();
  }
  catch (Exception e)
  {
    e.printStackTrace();
    System.out.println("exception");
  }
  finally
  {
    System.out.println("finally");  
  }
  System.out.println("after finally");    // 1
}
// 输出:
java.lang.Exception
    at Main.testTryCatch(Main.java:39)
    at Main.main(Main.java:10)
exception
finally
after finally

例3:
// 测试finally中的return优先级最高
private static String testTryCatch()
{
  try
  {
    throw new Exception();
  }
  catch (Exception e)
  {
    e.printStackTrace();
    return "exception";    // 2
  }
  finally
  {
    return "finally";        // 1    
  }
}
// 输出
最后返回的结果是// 1中的"finally"

例4:
public static void main(String[] args)
    {
        Test test = new Test();
        try
        {
            test.testTryCatch();
        }
        catch (Exception e)
        {
            System.out.println("exception");    // 3
        }
    }
   
    // 测试try/finally语句块抛出异常的执行顺序
    private void testTryCatch() throws Exception
    {
      try
      {
          System.out.println("try");        // 1
          throw new Exception();
      }
      finally
      {
        System.out.println("finally");      // 2   
      }
    }

// 输出:
try
finally
exception
也就是说,先执行try(// 1),然后执行finally(// 2),然后执行抛出的异常(// 3)

例5:
public static void main(String[] args)
    {
        Test test = new Test();
        try
        {
            test.testTryCatch();
        }
        catch (IOException e)
        {
            System.out.println("exception"); // 4
        }
    }
   
    // 测试catch中又抛出异常,其执行顺序
    private void testTryCatch() throws IOException
    {
      try
      {
          System.out.println("try");        // 1
          throw new Exception();
      }
      catch (Exception e)
      {
          System.out.println("catch before throw new exception");    // 2
          throw new IOException();
//          System.out.println("catch after throw IOException"); // 无法到达的代码
      }
      finally
      {
        System.out.println("finally");      // 3
      }
    }

输出:
try
catch before throw new exception
finally
exception
也就是说,先执行try(//1),再执行catch(//2),然后finally(//3),最后外部的catch(//4)。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多