
一个简单的java编程题,请大家帮帮忙!
编写一个class,内含good(),better()。请在better()中掷出你所定义的异常。在good()中调用better(),并捕获其异常。接着在catch子句...
编写一个class,内含good(),better()。请在better()中掷出你所定义的异常。在good()中调用better(),并捕获其异常。接着在catch子句中掷出另一异常。
展开
2个回答
展开全部
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();
}
}
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();
}
}
展开全部
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();
}
}
}
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();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询