首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

看 think in java 例子中有遇到个有关问题,请帮个忙

2012-03-24 
看 think in java 例子中有遇到个问题,请帮个忙//: Rethrowing.java// Demonstrating fillInStackTrace()p

看 think in java 例子中有遇到个问题,请帮个忙
//: Rethrowing.java 
// Demonstrating fillInStackTrace() 
package c09;

public class Rethrowing {

public static void f() throws Exception {
System.out.println("originating the exception in f()");

throw new Exception("thrown from f()");
}[color=#FF0000][/color]

public static void g() throws Throwable { //(2)********就是这里,Throwable 换成 Exception 就没问题了
try {
f();
} catch (Exception e) {
System.out.println("Inside g(), e.printStackTrace()");
e.printStackTrace();
throw e; // 17
// throw e.fillInStackTrace(); // 18
}
}

public static void main(String[] args)/* throws Throwable */{
try {
g(); // ****(1) 这里会出现 Unhandled exception type Throwable 错误
} catch (Exception e) {
System.out.println("Caught in main, e.printStackTrace()");
e.printStackTrace();
}
}
} // /:~ 

书中的例子我改了下,情况代码中的注释 (1),和 (2)

[解决办法]

Java code
public class Rethrowing {    public static void f() throws Exception {        System.out.println("originating the exception in f()");        throw new Exception("thrown from f()");    }    public static void g() throws Throwable { //(2)********就是这里,Throwable 换成 Exception 就没问题了        try {            f();        } catch (Exception e) {            System.out.println("Inside g(), e.printStackTrace()");            e.printStackTrace();            throw e; // 17// throw e.fillInStackTrace(); // 18        }    }    public static void main(String[] args)/* throws Throwable */{        try {            g(); // ****(1) 这里会出现 Unhandled exception type Throwable 错误//        } catch (Exception e) {//            System.out.println("Caught in main, e.printStackTrace()");//            e.printStackTrace();        }catch(Throwable t){            //如果go()可能抛出Throwable,应该用catch Throwable            //或者在main后面  throws Throwable             t.printStackTrace();        }    }} 

热点排行