当前位置: 代码迷 >> Eclipse >> 看 think in java 例子中有遇到个有关问题,请帮个忙
  详细解决方案

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

热度:632   发布时间:2016-04-23 14:02:36.0
看 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();        }    }}
  相关解决方案