Java_throw和throws的区别

目录

一、一览表

二、异常课后作业

一、一览表

意义位置后面跟的东西
throws异常处理的一种方式方法声明处异常类型
throw手动生成异常对象的关键字方法体中异常对象

二、异常课后作业

1.编写运用程序EcmDef.java接受命令行的两个参数,计算两数相除

2.计算两个数相除,要求使用方法cal(int n1,int n2)

3.对数据格式不正确(numberformatException),缺少命令行参数(ArrayIndexoutofException),除0进行异常处理

package com.chapter.customException_;

/**
 * @version 1.0
 * @auter liyang
 */
public class Homework01 {
    public static void main(String[] args) {



        try {
            if(args.length!=2){
                throw new ArrayIndexOutOfBoundsException("参数个数不对");
            }
            int n1=Integer.parseInt(args[0]);
            int n2=Integer.parseInt(args[1]);

            double res=cal(n1,n2);
            System.out.println("计算结果:"+res);

        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println(e.getMessage());
        }catch (NumberFormatException e){
            System.out.println("参数格式不正确,输入整数");
        }catch (ArithmeticException e){
            System.out.println("出现除0的异常");
        }
    }
    public static double cal(int n1,int n2){
        return n1/n2;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值