标题: [基础] Java程序异常处理的特殊情况
ljjk5
元帅
Rank: 1


荣誉会员奖章
UID 46706
精华 1
积分 99426
帖子 49690
威望 554
金币 48489
热心 505
阅读权限 100
注册 2007-2-25
状态 离线
Java程序异常处理的特殊情况

 1、不能在finally块中执行return,continue等语句,否则会把异常“吃掉”;  2、在try,catch中如果有return语句,则在执行return之前先执行finally块
  请大家仔细看下面的例子:
以下是引用片段:
publicclassTryTest{ 
  publicstaticvoidmain(String[]args){ 
    try{ 
      System.out.println(TryTest.test());//返回结果为true其没有任何异常 
    }catch(Exceptione){ 
      System.out.println("Exceptionfrommain"); 
      e.printStackTrace(); 
    } 
    doThings(0); 
  } 
 
  publicstaticbooleantest()throw*ception{ 
    try{ 
      thrownewException("Somethingerror");//第1步.抛出异常 
    }catch(Exceptione){//第2步.捕获的异常匹配(声明类或其父类),进入控制块 
      System.out.println("Exceptionfrome");//第3步.打印 
      returnfalse;//第5步.return前控制转移到finally块,执行完后再返回(这一步被吃掉了,不执行) 
    }finally{ 
      returntrue;//第4步.控制转移,直接返回,吃掉了异常 
    } 
  } 
   
  publicstaticvoiddoThings(inti) 
  { 
  try 
  { 
   if(i==0) 
   { 
   //在执行return之前会先执行finally 
   return; 
   } 
   intt=100/i; 
   System.out.println(t); 
  }catch(Exceptionex) 
  { 
   ex.printStackTrace(); 
  } 
  finally 
  { 
   System.out.println("finally"); 
  } 
  } 
}

网友 ljjk5 签名 - 网友社区 ===
顶部
[广告] 免费域名(Free Subdomain) 免费空间(Free hosting) PR查询(Google Pagerank)



当前时区 GMT+8, 现在时间是 2008-10-8 13:48
信产部ICP备案:京ICP备05066424号 北京市公安局网监备案:1101050648号

Powered by Discuz! 5.5.0
清除 Cookies - 联系我们 - 网友俱乐部 - Archiver - WAP