异常处理
package JAVA实验;
import java.util.*;
class MyException extends Exception{}
public class Sum{
public static void main(String[] args) {
int a = 0, sum = 0;
while(true) {
Scanner in = new Scanner(System.in);
boolean flag = true;
try{
a = in.nextInt();
} catch(InputMismatchException e) {
flag = false;
System.out.println("input mismatched");
}
try {
if(a < 0) {
throw new MyException();
}
} catch(MyException e) {
flag = false;
System.out.println("input a negative number");
a = 0;
}
if(!flag) {
continue;
}
if(a == 999) {
break;
}
sum += a;
}
System.out.println(sum);
}
}