public class Prac1{
public static void f() throws TestException{
System.out.println("throw exception from f()");
throw new TestException("!!!warning!!!");
}
public static void main(String[] args){
try{
f();
} catch(TestException te){
System.out.println(te.getMessage());
}
try{
String s = null;
s.length();
} catch(Exception e){
System.out.println(e);
}
finally{
System.out.println("into finally");
}
}
}
TestException是我自己定义的异常类。
我的问题是:
我在f()声明中不加throws TestException,那么编译就会出错,要我添加异常说明。
但是我第2个try-catch中,length方法也没加异常说明吧,我想。
同样,我在调用f()时不使用try-catch,编译就出错,要我添加异常处理。
但是我第2个try-catch中,加不加异常处理都可以。
为什么两者不同???