问题描述:编写程序ExceptionDemo.iava,接收命令行的两个参数,要求不能输入负数,计算两个数相除的值。
要求: 对以下异常进行捕捉。
。数据类型不一致:NumberFormatException
。缺少命令行参数:ArrayIndexOutOfBoundsException
。除0 ArithmeticException
。输入负数:自定义异常ExceptionDefine
**提示:
·主类(ExceptionDemo)中定义异常方法(ecm)完成两数相除功能。
。在main()方法中使用异常处理语句进行异常处理。
。在程序中,自定义对应输入负数的异常类(ExceptionDefine)。
。运行时接受参数 20 10 //args[0]=“20”args[1]="10”。
。Interger类的static方法parselnt(String s)将s转换成对应的int值。
。如:int a=Interger.parselnt(“314");
。 //a=314
测试类
public class ExceptionDemo {
public static void main(String[] args) {
int i=Integer.parseInt(args[0]);
int j=Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
System.out.println("数据转换错误!");
}catch (ExceptionDefine e) {
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("参数太少了!");
}catch (ArithmeticException e){
System.out.println("除数为0了!");
System.out.println(result);
public static int ecm(int a,int b) throws ExceptionDefine{
throw new ExceptionDefine("输入负数了,不行,重新输入!");
自定义的异常类
import javax.management.RuntimeOperationsException;
public class ExceptionDefine extends RuntimeException {
static final long serialVersionUID = -90745766939L;
public ExceptionDefine() {
public ExceptionDefine(String message) {