首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

错误有关问题

2012-01-29 
异常问题What is the minimal list of exception classes that the overriding method f() in the followi

异常问题
What is the minimal list of exception classes that the overriding method f() in the following code must declare in its throws clause before the code will compile correctly? 
class A{ 
//InterruptedException is a direct subclass of Exception. 
void f() throws ArithmeticException, InterruptedException{ 
div(5,5); 

int div(int i, int j)throws ArithmeticException{ 
return i/j; 


public class MyClass extends A{ 
void f()/*throws [list of exceptions]*/{ 
try{ 
div(5,0); 
}catch(ArithmeticException e){ 
return; 
`} 
throw new RuntimeException("ArithmeticException was expected"); 




[解决办法]
不用写吧?只要处理好异常就行了
[解决办法]
应该没问题呀,我试了,可以的.
你再试下这样改 : void f()throws RuntimeException
[解决办法]
class A{
//InterruptedException is a direct subclass of Exception.
void f() throws ArithmeticException, InterruptedException{
div(5,5);
}
int div(int i, int j)throws ArithmeticException{
return i/j;
}
}
public class ExceptionTest extends A{
void f()/*throws [list of exceptions]*/{
System.out.println("----begin----");
try{
div(5,0);
}catch(ArithmeticException e){
//return; 
e.printStackTrace();
}
System.out.println("----end----");
//throw new RuntimeException("ArithmeticException was expected");
}
public static void main(String[] args){
ExceptionTest test = new ExceptionTest();
test.f();
}
}
[解决办法]
MyClass中的f()不需要再声明异常了。不过调用f()的方法需要处理或者声明InterruptedException。

热点排行