JAVA求修改,自定义两个异常类来描述可能发生的异常,捕获异常,对它们进行处理。

2025-04-24 21:55:50
推荐回答(1个)
回答1:

package com.exception;
public class Main {
public static void main(String args[]){
try{
if(args.length!=2){
throw new ParameterNumberException();
}else{
if(!check(args[0])||!check(args[1])){
throw new ParameterFormatException();
}
}
}catch(ParameterNumberException e){
System.out.println(e);
e.printStackTrace();
}catch(ParameterFormatException e){
System.out.println(e);
e.printStackTrace();
}
}
//检测输入的字符串是否都是整数
public static boolean check(String s){
for(int i=0;i if(!Character.isDigit(s.charAt(i))){
return false;
}
}
return true;
}
}
class ParameterNumberException extends Exception{

ParameterNumberException(){
super("参数个数异常!");
}
public String toString(){
return "ParameterNumberException";
}

}
class ParameterFormatException extends Exception{
ParameterFormatException(){
super("参数格式异常!");
}
public String toString(){
return "ParameterFomatException";
}
}