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
我想知道为什么。
解决方案 »
- 出现异常之后怎么判断程序是否继续进行
- 求java经典教材!
- 一个非常怪异的打包问题.
- 关于这个程序,大家来看看
- JSP上传图片
- 各位JAVA高手帮忙,学校的assignment,一点头绪也没有
- canvas 中paint()调用
- <JAVA编程思想》里的一段代码,按照书的指示完整的打上去,可竟然发现了20个ERROR!
- 谁有jbuilder2006的教程?我在jbuilder2006里建一个web module,然后在web module里建了一个jsp和servlet还有配置了一个mapping.可是我的js
- 在linux下怎样安装JAVA编辑软件?
- 数组连接
- 请教高手,java中如何为一个数组重新分配堆内存
假设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()");
}
}
} ///:~那么这段错哪了呢?达人救我。