
java中使用throws无法抛出异常。代码如下:
publicclassimmortal{publicvoidspell(inti)throwsException{if(i==5){thrownewException()...
public class immortal {
public void spell(int i) throws Exception {
if(i == 5){
throw new Exception();
}
System.out.println("the" + i + "success");
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
immortal monkey = new immortal();
for (int i = 0 ; i < 7 ; i++){
monkey.spell(i);
}
}
}
提示No exception of type Exception can be thrown。。。。。java7的 展开
public void spell(int i) throws Exception {
if(i == 5){
throw new Exception();
}
System.out.println("the" + i + "success");
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
immortal monkey = new immortal();
for (int i = 0 ; i < 7 ; i++){
monkey.spell(i);
}
}
}
提示No exception of type Exception can be thrown。。。。。java7的 展开
展开全部
运行结果
Exception in thread "main" java.lang.Exception
at Test.spell(Test.java:4)
at Test.main(Test.java:14)
the0success
the1success
the2success
the3success
the4success
i等于5时抛出了异常
Exception in thread "main" java.lang.Exception
at Test.spell(Test.java:4)
at Test.main(Test.java:14)
the0success
the1success
the2success
the3success
the4success
i等于5时抛出了异常
追问
你是怎么运行的?java1.6?还是1.7?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这段代码在java6中肯定没问题,有何能是java7版本的新机制吧~
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你看下你的设置用的是哪个版本的JDK
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询