RT..代码如下:代码一:
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 {
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) throws Throwable {
try {
g();
}catch(Exception e) {
System.out.println("Caught in main(), e.printStackTrace()");
e.printStackTrace();
}
}
}
代码二:
public class FullConstructors {
public static void f() throws MyException {
System.out.println("Throwing MyException from f()");
throw new MyException();
}
public static void main(String[] args) {
try{
f();
}catch(MyException e) {
e.printStackTrace();
}
}
}
如上. 在代码一中main()方法那行如果去掉 throws Throwable 编译时就会出错:
Rethrowing.java:28: unreported exception java.lang.Throwable; must be caught or
declared to be thrown
代码二的main()方法里没有throws声明却不会.请问为什么代码一中一定要throws声明..?谢谢.
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 {
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) throws Throwable {
try {
g();
}catch(Exception e) {
System.out.println("Caught in main(), e.printStackTrace()");
e.printStackTrace();
}
}
}
代码二:
public class FullConstructors {
public static void f() throws MyException {
System.out.println("Throwing MyException from f()");
throw new MyException();
}
public static void main(String[] args) {
try{
f();
}catch(MyException e) {
e.printStackTrace();
}
}
}
如上. 在代码一中main()方法那行如果去掉 throws Throwable 编译时就会出错:
Rethrowing.java:28: unreported exception java.lang.Throwable; must be caught or
declared to be thrown
代码二的main()方法里没有throws声明却不会.请问为什么代码一中一定要throws声明..?谢谢.
解决方案 »
- 求一些 JTable表格更改事件的示例代码
- JUnit和Mock老手进来帮忙看看
- JAVA小问题,没有满意的答案,在发一次,请高人进来!谢谢
- 页面的加了这个上传的enctype="multipart/form-data,怎么取字段的数组值,不能request.getParameterValues(""),急!!!
- 关于流与其他类型的转化
- extends FileWriter出错(JDK 1.5.0)
- juilder问题,急!
- 提一个easy的问题?
- 利用Applet做Client,打开一个Socket连接,则出现java.security.AccessControlException: access denied 问题,有什么简便的方法解决???救命啊,立刻给分啊~~
- 最酷的话题___java做聊天室,有代码者加分。
- jmf中怎么样设置输出视频大小
- 如何将MYSQL数据库和JBuilder9连接
这一行
表明g()会抛出一个Throwable异常而g()中的catch(Exception e)中的Exception e异常不是g()自己抛出的,而是f()抛出的
但是,g()也抛出了Exception异常,是因为g()在处理f()抛出的Exception异常的时候也抛出了Exception异常,如楼主代码"//17"处....那么g()实际上抛出了Throwable和Exception类型的两个异常
main()中仅仅catch了Exception异常而没有catch Throwable异常或者再次将Throwable异常抛出....所以有错误产生
g();
} catch(Throwable e) {
//....
}或者:try {
g();
} catch(Exception e) {
//...
} catch(Throwable e) {
//....
}均可。
try {
g();
}catch(Exception e) {
System.out.println("Caught in main(), e.printStackTrace()");
e.printStackTrace();
}
}
中的throws Throwable 去掉应该就可以了,没有抛出新的exception
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 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.out.println("Caught in main(), e.printStackTrace()");
e.printStackTrace();
}
}
}
public static void g() throws Throwable 这个虽然有声明g()方法可能会抛出Throwable异常,但是实际上在g()方法里并没有抛出Throwable异常,而只是抛出了Exception异常. 我不能理解的是为什么g()也会抛出Throwable异常. 异常不是类似这样throw new Exception("thrown from f()"); 才会抛出吗..?
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 {
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.out.println("Caught in main(), e.printStackTrace()");
e.printStackTrace();
}catch(Throwable t) {
System.out.println("测试捕捉Throwable异常.");
}
}
}但输出并没有"测试捕捉Throwable异常."这句, 是没有捕捉到Throwable异常..? 还是g()方法跟本没有抛出Throwable异常 ...?
//没有抛出任何异常
}但是由于它声明了一个Exception异常,所以在调用f()的时候,必须捕获Exception。
g();
}catch(Exception e) {
System.out.println("Caught in main(), e.printStackTrace()");
e.printStackTrace();
}catch(Throwable t) {
System.err.println("测试捕捉Throwable异常.");
}