JAVA---异常练习

/*
 * 编写应用程序EcmDef.java,接收命令行的两个参数,要求不能输入负数,计算两数相除。
	对数据类型不一致(NumberFormatException)、缺少命令行参数(ArrayIndexOutOfBoundsException、
  	除0(ArithmeticException)及输入负数(EcDef 自定义的异常)进行异常处理。
提示: 
	(1)在主类(EcmDef)中定义异常方法(ecm)完成两数相除功能。
	(2)在main()方法中使用异常处理语句进行异常处理。
	(3)在程序中,自定义对应输入负数的异常类(EcDef)。
	(4)运行时接受参数 java EcmDef 20 10   
	(5)Interger类的static方法parseInt(String s)将s转换成对应的int值。    
 */
public class EcmDef {
	public static void main(String[] args) {
		try {
			int i=Integer.parseInt(args[0]);
			int j=Integer.parseInt(args[1]);
			int result=ecm(i,j);
			System.out.println(result);
		} catch (NumberFormatException e) {
			System.out.println("数据类型不一致");
		} catch (EcDef e) {
			System.out.println(e.getMessage());
		} catch(ArrayIndexOutOfBoundsException e){
			System.out.println("缺少命令行参数");
		} catch(ArithmeticException e){
			System.out.println("除0");
		}
	}
	public static int ecm(int i,int j) throws EcDef{
		if(i<0||j<0){
			throw new EcDef("分子或分母为负数了!");
		}
		return i/j;
	}
}

package exer;

public class EcDef extends Exception{
	static final long serialVersionUID=-2222222L;
	public EcDef(){
		
	}
	public EcDef(String msg){
		super(msg);
	}
}

posted @   ice--cream  阅读(120)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2021-02-22 css选择器(1)
点击右上角即可分享
微信分享提示