A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

©   /  2013-4-21 16:54  /  1419 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


  1. public class ExceptionTest {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.                 // TODO Auto-generated method stub
  7.                 try {
  8.                         throw new RuntimeException("程序正常代码异常,退出jvm。");
  9.                 } catch (Exception e) {
  10.                        
  11.                         System.out.println(e.toString());
  12.                 }
  13.                 finally {
  14.                         /**
  15.                          * 1、关闭资源放到finally代码块中,不管try代码抛出什么异常,这里的代码都会执行。
  16.                          * 2、finally代码块中前面的的语句发生异常,不影响后面的正常执行。
  17.                          * 3、从第一个关资源try看出,要关闭多个资源,一定要分开try。
  18.                          */
  19.                         try {
  20.                                 throw new RuntimeException("资源一关闭异常");
  21.                                 //System.out.println("看看我有被执行吗?");//这句话编译不通过,说明他不会被执行
  22.                         } catch (Exception e) {
  23.                                
  24.                                 System.out.println(e.toString());
  25.                         }
  26.                        
  27.                         try {
  28.                                 throw new RuntimeException("资源二关闭异常");
  29.                         } catch (Exception e) {
  30.                                
  31.                                 System.out.println(e.toString());
  32.                         }
  33.                        
  34.                         try {
  35.                                 throw new RuntimeException("资源二关闭异常");
  36.                         } catch (Exception e) {
  37.                                
  38.                                 System.out.println(e.toString());
  39.                         }
  40.                 }

  41.         }

  42. }
复制代码
执行结果:
java.lang.RuntimeException: 程序正常代码异常,退出jvm。
java.lang.RuntimeException: 资源一关闭异常
java.lang.RuntimeException: 资源二关闭异常
java.lang.RuntimeException: 资源二关闭异常

回复 使用道具 举报
Neverbelazy 发表于 2013-4-28 14:34
我理解你的意思,你的意思是 try{ 中的 throw new RuntimeException();} 是否会被catch; 你的这段 try-cat ...

哥们你太纠结了,
你想想当你关闭资源失败时,你能做什么?
其实你是处理不了这个异常的,一般情况下士要把这个异常抛给你的调用方去处理的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马