日期:2014-05-20  浏览次数:20845 次

异常问题
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。