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.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();
}
catch(Exception e)
{
System.out.println("Caught in main, e.printStackTrace()");
e.printStackTrace();
}
}
}如上. 在main()方法那行如果去掉 throws Throwable 编译时就会出错:
Rethrowing.java:28: unreported exception java.lang.Throwable; must be caught or
declared to be thrown
但是mian()方法里面并没有抛出异常为什么要有异常说明(throws) ? 谢谢.
{
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.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();
}
catch(Exception e)
{
System.out.println("Caught in main, e.printStackTrace()");
e.printStackTrace();
}
}
}如上. 在main()方法那行如果去掉 throws Throwable 编译时就会出错:
Rethrowing.java:28: unreported exception java.lang.Throwable; must be caught or
declared to be thrown
但是mian()方法里面并没有抛出异常为什么要有异常说明(throws) ? 谢谢.
public static void g() throws Throwablemian()方法调用g()方法为什么还要做异常说明呢..?
在f()中throw new Exception("thrown from f()");
否则当g抛出Throwable的时候,main又没有catch到,那main也必须向上抛出啊,因为到最后要找到一个处理者
exception 是 throwable的子类,所以你的catch 没有catch到g抛出的throwable
public static void g()
{}那就不用了,因为g()没有抛出异常!