1.计算n!并且捕获所有可能出现的异常
public class Example6_10 {
public static int multi(int n){
if(n<0){
throw new IllegalArgumentException("求负数阶乘异常");
}
int m=1;
for(int i=1;i<=n;i++){
m=m*i;
}
return m;
}
public static void main(String[] args) {
try {
int m =Integer.parseInt(args[0]);
System.out.println(m+"!="+multi(m));
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("命令行中没提供参数!");
}catch (NumberFormatException e){
System.out.println("应输入一个整数!");
}catch (IllegalArgumentException e){
System.out.println("出现的异常是:"+e.toString());
}finally{
System.out.println("程序运行结束!");
}
}
}
|