Chinaunix首页 | 论坛 | 博客
  • 博客访问: 820768
  • 博文数量: 780
  • 博客积分: 7000
  • 博客等级: 少将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-12 09:11
文章分类

全部博文(780)

文章存档

2011年(1)

2008年(779)

我的朋友
最近访客

分类:

2008-09-12 09:14:48

以下是引用片段:
public class TryTest { 
    public static void main(String[] args) { 
        try { 
            System.out.println(TryTest.test());// 返回结果为true其没有任何异常 
        } catch (Exception e) { 
            System.out.println("Exception from main"); 
            e.printStackTrace(); 
        } 
        doThings(0); 
    } 
 
    public static boolean test() throws Exception { 
        try { 
            throw new Exception("Something error");// 第1步.抛出异常 
        } catch (Exception e) {// 第2步.捕获的异常匹配(声明类或其父类),进入控制块 
            System.out.println("Exception from e");// 第3步.打印 
            return false;// 第5步. return前控制转移到finally块,执行完后再返回(这一步被吃掉了,不执行) 
        } finally { 
            return true; // 第4步. 控制转移,直接返回,吃掉了异常 
        } 
    } 
     
    public static void doThings(int i) 
    { 
     try 
     { 
      if(i==0) 
      { 
       //在执行return之前会先执行finally 
       return; 
      } 
      int t=100/i; 
      System.out.println(t); 
     }catch(Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
     finally 
     { 
      System.out.println("finally"); 
     } 
    } 

--------------------next---------------------

阅读(277) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~