我看了侯捷写的Java编程思想后,对于'通过异常处理错误‘这一章有一个地方想不通,在原书中的P392的Rethrowing.java与P394的ThrowOut.java使得我对Throwable与Excepiton的关系产生了疑惑,我查了帮助文档说了Throwable是Exception的父类,当P392的Rethrowing.java却使我不明白了,请看Rethowing.java的源代码:
public class Rethowing
{
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; //(1)
throw e.fillInStackTrace(); //(2)
}
}
public static void main(String[] args) throws Throwable
{
try
{
g();
}
catch(Exception e)
{
System.err.println(
"Caught in main,e.printStackTrace()");
e.printStackTrace();
}
}
}
当将(1)注释掉结果为:
originating the exception in f()
Inside g(),e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethowing.f(Rethowing.java:6)
at Rethowing.g(Rethowing.java:13)
at Rethowing.main(Rethowing.java:29)
Caught in main,e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethowing.g(Rethowing.java:20)
at Rethowing.main(Rethowing.java:29)
当将(2)注释掉结果为:
originating the exception in f()
Inside g(),e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethowing.f(Rethowing.java:6)
at Rethowing.g(Rethowing.java:13)
at Rethowing.main(Rethowing.java:29)
Caught in main,e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethowing.f(Rethowing.java:6)
at Rethowing.g(Rethowing.java:13)
at Rethowing.main(Rethowing.java:29)
疑惑:
1。当将(1)注释掉时,调用throw e.fillInStackTrace();时,将抛出一个Throwable型的引用这可以理解,但当这个Throwable引用抛出到main()函数时,却能与Exception匹配,运行catch(Exception e)中的语句!!!!
Throwable不是Exception的父类吗???为什么从父类转型到子类不需要强制转换,就可以匹配成功???
当我硬着头皮往下看是,更加迷糊了,请见P394的ThrowOut.java源代码:
public class ThrowOut
{
public static void main(String[] args) throws Throwable
{
try
{
throw new Throwable();
}
catch(Exception e)
{
System.out.println("caught in main()");
}
}
}
在ThrowOut.java中,抛出一个Throwable()去不会与Exception匹配,这不是跟上面的情况矛盾吗???
2。在Rethowing.java中的f()与g()成员函数中都调用了e.printStackTarce(),为什么两次的输出结果都一样,难道Java这么有预见性,还没将异常抛到上层就可以知道其行动路线??? 这是原帖
http://topic.csdn.net/t/20030403/11/1612923.html
public class Rethowing
{
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; //(1)
throw e.fillInStackTrace(); //(2)
}
}
public static void main(String[] args) throws Throwable
{
try
{
g();
}
catch(Exception e)
{
System.err.println(
"Caught in main,e.printStackTrace()");
e.printStackTrace();
}
}
}
当将(1)注释掉结果为:
originating the exception in f()
Inside g(),e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethowing.f(Rethowing.java:6)
at Rethowing.g(Rethowing.java:13)
at Rethowing.main(Rethowing.java:29)
Caught in main,e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethowing.g(Rethowing.java:20)
at Rethowing.main(Rethowing.java:29)
当将(2)注释掉结果为:
originating the exception in f()
Inside g(),e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethowing.f(Rethowing.java:6)
at Rethowing.g(Rethowing.java:13)
at Rethowing.main(Rethowing.java:29)
Caught in main,e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethowing.f(Rethowing.java:6)
at Rethowing.g(Rethowing.java:13)
at Rethowing.main(Rethowing.java:29)
疑惑:
1。当将(1)注释掉时,调用throw e.fillInStackTrace();时,将抛出一个Throwable型的引用这可以理解,但当这个Throwable引用抛出到main()函数时,却能与Exception匹配,运行catch(Exception e)中的语句!!!!
Throwable不是Exception的父类吗???为什么从父类转型到子类不需要强制转换,就可以匹配成功???
当我硬着头皮往下看是,更加迷糊了,请见P394的ThrowOut.java源代码:
public class ThrowOut
{
public static void main(String[] args) throws Throwable
{
try
{
throw new Throwable();
}
catch(Exception e)
{
System.out.println("caught in main()");
}
}
}
在ThrowOut.java中,抛出一个Throwable()去不会与Exception匹配,这不是跟上面的情况矛盾吗???
2。在Rethowing.java中的f()与g()成员函数中都调用了e.printStackTarce(),为什么两次的输出结果都一样,难道Java这么有预见性,还没将异常抛到上层就可以知道其行动路线??? 这是原帖
http://topic.csdn.net/t/20030403/11/1612923.html
解决方案 »
- Exception in thread "main" java.lang.NoClassDefFoundError:(大家帮我看看这个程序。。。)
- 好奇就进来看看吧!!!!!
- linux中,java访问局域图片问题,请大家多多帮忙!~
- 帮看看 socket有什么问题
- 使用JMF框架建立视频会议时产生的问题!!!很急,有精通的高手帮忙帮忙
- 有没有类似于msdn的java帮助啊?
- 200分想和大家讨论一个问题:为什么有些人会说搞数据库开发只是表面的东西,技术含量不高,真正的程序员要做底层开发。
- 帮忙改错???不明白呀!!!
- 请问如何消除一个字符串中的换行符?
- Hello world程序简单问题??
- Hibernate里save()、update()、saveOrUpdate()、merge()区别?
- jdbc连接oracle出错,烦劳大家帮我看看
2:e.printStackTarce()显示的是原始异常抛出的地方的信息,不管抛多少层都是,所以两次输出的结果是一样的,
调用e.fillInStackTrace()后,把g()作为异常抛出点了,就不是f()了,所以会少打印一个f()的信息。
希望有人解答第一个。
他的意思是在第一个g()中没必要抛到Main中来? 是这个意思吗?
他的意思是不是:把(2)注释掉之后,输出结果应该是:
originating the exception in f()
Inside g(),e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethowing.f(Rethowing.java:6)
at Rethowing.g(Rethowing.java:13)
at Rethowing.main(Rethowing.java:29)
Caught in main,e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethowing.f(Rethowing.java:6)
at Rethowing.g(Rethowing.java:13)
at Rethowing.main(Rethowing.java:29)
红色部分是不是应该去掉?
的调用,因为把g()当作异常抛出点了。
public static main(String[] sdf){
System.out.println("Hello World!");
}
public static void g() throws Throwable {
// ...
}public static void main(String[] args) throws Throwable
{
try
{
g();
}
catch(Exception e)
{
System.err.println(
"Caught in main,e.printStackTrace()");
e.printStackTrace();
}
}
}
这里根本编译都通过不了。