Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209489
  • 博文数量: 38
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-09 12:32
文章分类

全部博文(38)

文章存档

2011年(1)

2008年(12)

2007年(25)

我的朋友

分类: Java

2008-07-21 20:15:20

Java的异常处理同样存在着瑕疵.(读Thinking in Java Found Edition有感)

实例一

package edu.dlut.zxf.test;

class VeryImportantException extends Exception {
    public String toString() {
        return "A very important exception!";
    }
}

class AnotherException extends Exception {
    public String toString() {
        return "Another Exception";
    }
}

public class LostMessageTest {
    public void f() throws VeryImportantException {
        throw new VeryImportantException();
    }
    
    public void g() throws AnotherException {
        throw new AnotherException();
    }
    
    public static void main(String[] args) {
        LostMessageTest test = new LostMessageTest();
        try {
            try {
                test.f(); //抛弃的异常VeryImportantException没有被catch

            } finally {
                test.g();
            }
        } catch(Exception e) {
            System.out.println(e.toString()); //此处catch的是AnotherException

        }
    }
}
/* Output:
Another Exception
*/



实例二

package edu.dlut.zxf.test;

public class LostMessageTest2 {
    public static void main(String[] args) {
        try {
            throw new Exception();
        } finally {
            return;
        }
    }
}

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