初学者请教
我是JAVA初学者,在学自定义的例外类时遇到如下问题:
下面是一个自定义的例外:
class MyException extends Exception{
static int InstanceNumber=0;
MyException(int j) throws MyException{
if (j%2){
throw new MyException();
InstanceNumber++;
}
}
String Say(){
return "I'm MyException."+InstanceNumber+"!";
}
}
class UseMyException{
public static void main (String[] args)
{
MyException My=new MyException( );
int i;
System.out.println ("test my exception");
for (i=0;i<7;i++){
try{
My.MyException(i);
}
catch(MyException e){
System.out.println("A exception\n"+My.Say());
}
System.out.println("test end.");
}
}
}
输出结果是:
Test my exception:
A Exception!
I'm MyException!
A Exception!
I'm MyException!
A Exception!
I'm MyException!
test End!
为什么“test end"只输出一次呢?不是在for 循环之内吗?
代码是清华大学的《VJ6++入门提高》中的。