栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA异常作业(Chapter12)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA异常作业(Chapter12)

第十二章作业

第十二章作业
  • 第十二章作业
    • 1.编程,使用try catch 处理异常
    • 2、3、4.读代码

1.编程,使用try catch 处理异常
package com.exception;


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]);
            System.out.println(cal(n1,n2));
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        } catch (NumberFormatException e){
            System.out.println("数字格式不正确。");
        }catch (ArithmeticException e){
            System.out.println("被除数不能是0。");
        }


    }
    public static int cal(int n1, int n2){
        return n1 / n2;
    }
}
2、3、4.读代码

运行异常:

  1. NullPointerException 空指针异常
  2. ArithmeticException 数学运算异常
  3. ArrayIndexOutOfBoundsException 数组下标越界异常
  4. ClassCastException 类型转换异常
  5. NumberFormatException 数字格式不正确异常[]

运行时finally的代码块一定会被执行。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/872160.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号