你的Sum(int n) 方法没有把异常抛出, 应该是下面这样
public class Exception3 {
public static int Sum(int n) throws IllegalArgumentException {
if (n < 0)
throw new IllegalArgumentException("n应该为整数");
int s = 0;
for (int i = 0; i <= n; i++)
s += i;
return s;
}
public static void main(String args[]) {
try {
int n = Integer.parseInt(args[0]);
System.out.println(Sum(n));
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("命令行为:" + "javaException3<number>");
} catch (NumberFormatException e2) {
System.out.println("参数<number>应为整数!");
} catch (IllegalArgumentException e3) {
System.out.println("错误参数:" + e3.toString());
} finally {
System.out.println("程序结束");
}
}
} |