public class Rethrowing {
public static void f() throws Exception {
System.out.println("originating the exception in f()");
throw new Exception();
}
public static void g() throws Exception
try {
f();
} catch(Exception e) {
System.err.println("Inside g(),e.printStackTrace()");
e.printStackTrace();
throw e; // 17
// throw e.fillInStackTrace(); // 18
}
}
public static void
main(String[] args) {
try {
g();
} catch(Exception e) {
System.err.println(
"Caught in main, e.printStackTrace()");
e.printStackTrace();
}
}
} ///:~
我发现当我把public static void g() throws Exception改成public static void g() throws Throwable
那么下面的MAIN 也要加throws Throwable
我想知道为什么。
public static void f() throws Exception {
System.out.println("originating the exception in f()");
throw new Exception();
}
public static void g() throws Exception
try {
f();
} catch(Exception e) {
System.err.println("Inside g(),e.printStackTrace()");
e.printStackTrace();
throw e; // 17
// throw e.fillInStackTrace(); // 18
}
}
public static void
main(String[] args) {
try {
g();
} catch(Exception e) {
System.err.println(
"Caught in main, e.printStackTrace()");
e.printStackTrace();
}
}
} ///:~
我发现当我把public static void g() throws Exception改成public static void g() throws Throwable
那么下面的MAIN 也要加throws Throwable
我想知道为什么。
假设g()会抛出一个IOException,在main()中就可以用IOException或者用Exception去捕获,因Exception 比IOException范围大。
那异常说明就是throws Throwable
如果是因为捕捉的和异常说明的不一致
而强行要求MAIN也说明为throws Throwable 。public class ThrowOut {
public static void
main(String[] args) throws Throwable {
try {
throw new Throwable();
} catch(Exception e) {
System.err.println("Caught in main()");
}
}
} ///:~那么这段错哪了呢?达人救我。