以下java程序不能运行,在不改动class Test的前提下,能不能编辑正确呢?请各位大虾帮忙,万分感谢!
class AException extends Exception{
public AException(){
super();
}
public String toString(){
String aa = "aaaaaaaa";
return aa;
}
}
class TestException{
void fund(int i)throws AException {
if(i==0)
throw new AException();
}
}
class Test{
public static void main(String[] arg){
TestException xx = new TestException();
xx.fund(0);
}
}
class AException extends Exception{
public AException(){
super();
}
public String toString(){
String aa = "aaaaaaaa";
return aa;
}
}
class TestException{
void fund(int i)throws AException {
if(i==0)
throw new AException();
}
}
class Test{
public static void main(String[] arg){
TestException xx = new TestException();
xx.fund(0);
}
}
所以不改变Test不能编译正确。
除非让fund函数不抛异常!
改成class AException extends RuntimeException
答案是将TestException用try/catch包住:
class TestException { void fund(int i) { if(i==0)
try {
throw new AException();
} catch (AException e) {
e.printStackTrace();
} } }
这个你也想的出来,明显是高手。请问为什么运行时会出现这个提示呢?
Exception in thread "main" aaaaaaaa
at TestException.fund(TestException.java:4)
at Test.main(Test.java:4)