一个简单的java编程题,请大家帮帮忙!

编写一个class,内含good(),better()。请在better()中掷出你所定义的异常。在good()中调用better(),并捕获其异常。接着在catch子句中掷出另一异常。

public class er{
public void good() throws [the other excpetion]{
try{
better();
}catch(Throwable e){
throw [the other excpetion]
}
}
public void better() throws [some class extends java.lang.Throwable]{
throw [some class extends java.lang.Throwable];
}
}
例如
public class er{
public void good() throws java.lang.IllegalAccessException{
try{
better();
}catch(java.sql.SQLException e){
throw new java.lang.IllegalAccessException("数据库错误");
}
}
public void better() throws java.sql.SQLException{
throw new java.sql.SQLException();
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2008-11-24
public class Test1 {

public void good() throws MyException {
try {
better();
} catch (Exception other_exception) {
other_exception.printStackTrace();
}
}

public void better() throws MyException {
throw new MyException("exception");
}

}
//自定义异常
public class MyException extends Exception {

public MyException(String msg){
super(msg);
}
public String toString()
return super.toString();
}
}

}
相似回答
大家正在搜