日期:2014-05-20 浏览次数:20941 次
import java.io.*;
public class TestNum {
public static boolean prime(int n) throws ArgumentOutOfBoundException{
if (n < 0)
throw new ArgumentOutOfBoundException();//n小于0抛出异常
if (n == 0){
return false;
}
int m =(int)Math.sqrt(n);
for(int i = 2;i <= m;i++){
if (n % i == 0){
return false;
}
}
return true;
}
public static void main(String[] args) {
try {
boolean flag = false;
flag = prime(-4);
System.out.println(flag);
} catch (ArgumentOutOfBoundException e) {//抛出异常后捕获,然后打印"参数不能小于零"
System.out.println("参数不能小于零!");
}
}
}
class ArgumentOutOfBoundException extends Exception {//定义自己的异常类
public ArgumentOutOfBoundException(){}
}