请教: java抛出异常问题

jedfish 2010-05-22 03:28:05
以下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);
}
}
...全文
83 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
龙四 2010-05-22
  • 打赏
  • 举报
回复
class AException extends Exception


改成

class AException extends RuntimeException
colin_pxx 2010-05-22
  • 打赏
  • 举报
回复
要么让main函数抛出异常要么对fund函数try catch
coolbamboo2008 2010-05-22
  • 打赏
  • 举报
回复
对xx.fund(0);一定要try catch,或者让main函数抛出异常
tristan0602 2010-05-22
  • 打赏
  • 举报
回复
你的fund函数抛出了异常,而你main函数中没有处理异常,也没有把异常抛出,那编译器肯定报错的!
所以不改变Test不能编译正确。

除非让fund函数不抛异常!
jedfish 2010-05-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ticmy 的回复:]
引用 7 楼 jedfish 的回复:

引用 4 楼 ticmy 的回复:
class AException extends Exception


改成

class AException extends RuntimeException


这个你也想的出来,明显是高手。请问为什么运行时会出现这个提示呢?
Exception in thread "main" aaaa……
[/Quote]
不一定啊,除了Test类,另外两个文件都可以改动。
龙四 2010-05-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jedfish 的回复:]

引用 4 楼 ticmy 的回复:
class AException extends Exception


改成

class AException extends RuntimeException


这个你也想的出来,明显是高手。请问为什么运行时会出现这个提示呢?
Exception in thread "main" aaaaaaaa
at TestException……
[/Quote]


OMG!你不是故意throw new AException()的么
jedfish 2010-05-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ticmy 的回复:]
class AException extends Exception


改成

class AException extends RuntimeException
[/Quote]

这个你也想的出来,明显是高手。请问为什么运行时会出现这个提示呢?
Exception in thread "main" aaaaaaaa
at TestException.fund(TestException.java:4)
at Test.main(Test.java:4)
jedfish 2010-05-22
  • 打赏
  • 举报
回复
我已经找到答案啦,谢谢各位,哈哈!~~~
答案是将TestException用try/catch包住:


class TestException {

void fund(int i) {

if(i==0)
try {
throw new AException();
} catch (AException e) {
e.printStackTrace();
}

}

}
donkee8 2010-05-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ticmy 的回复:]
class AException extends Exception


改成

class AException extends RuntimeException
[/Quote]

同意,都支持!

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧