1Java加强----异常

1.异常体系

1.1异常入门

1.1运行时异常

   public static void show(){
//        int[] arr={1,2,3};
//        System.out.println(arr[3]);//.ArrayIndexOutOfBoundsException数组越界异常
//
        String str = null;
        System.out.println(str.length());//NullPointerException空指针异常
    }

运行时才会报错

产生异常时,后面的代码不会执行

1.2编译时异常

见名知义,编译时就会报错

 public static void show2(){
        System.out.println("==程序开始。。。。==");
        // 编译异常:编译阶段报错,编译不通过。  已知字符串时间不能转成时间对象进行处理
        String str = "2024-07-09 11:12:13";
        // 把字符串时间解析成Java中的一个日期对象。
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = sdf.parse(str); // 编译时异常,提醒程序员这里的程序很容易出错,请您注意! 担心您上面这两行怕您出错,怕格式不一样

    }

1.3异常的基本处理

try {
            // 监视代码,出现异常,会被catch拦截住这个异常
            show2();
        } catch (Exception e) {
            e.printStackTrace(); // 打印这个异常信息
        }


public static void show2() throws Exception

2.异常的作用

2.1作为方法的特殊返回值

public class Demo2 {
    public static void main(String[] args) {
        System.out.println(div(10, 0));
    }

    private static int div(int a, int b) {
        if (b == 0) {
            return -1;
        }
        int result = a / b;
        return result;
    }
}

上面这个代码,当b等于0时,没必要再往下执行,而且往下执行会报红. 但是又必须返回一个值,一般都返回负一.

想要这个有意义的话,就得引出我们的异常啦

public class ExceptionDemo2 {
    public static void main(String[] args) {
        // 目标:搞清楚异常的作用。
        System.out.println("程序开始执行...");
        try {
            System.out.println(div(10, 0));
            System.out.println("底层方法执行成功了~~~");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("底层方法执行失败了~~");
        }
        System.out.println("程序结束执行...");
    }

    // 需求:求2个数的除的结果,并返回这个结果。
    public static int div(int a, int b) throws Exception {//throws Exception更加强烈
        if(b == 0){
            
            // 可以返回一个异常给上层调用者,返回的异常还能告知上层底层是执行成功了还是执行失败了!!
            throw new Exception("除数不能为0,您的参数有问题!");
        }
        int result = a / b;
        return result;
    }
}

此时

3.自定义异常

3.1自定义编译时异常

首先定义异常

public class ItheimaAgeIllegalException extends Exception{
    public ItheimaAgeIllegalException() {
    }

    public ItheimaAgeIllegalException(String message) {
        super(message);
    }
}
public class ExceptionDemo3 {
    public static void main(String[] args) {
        // 目标:认识自定义异常。
        System.out.println("程序开始。。。。");
        try {
            saveAge(300);
            System.out.println("成功了!");
        } catch (ItheimaAgeIllegalException e) {
            e.printStackTrace();
            System.out.println("失败了!");
        }
        System.out.println("程序结束。。。。");
    }

    // 需求:我们公司的系统只要收到了年龄小于1岁或者大于200岁就是一个年龄非法异常。
    public static void saveAge(int age) throws ItheimaAgeIllegalException {
        if(age < 1 || age > 200){
            // 年龄非法;抛出去一个异常。
            throw new ItheimaAgeIllegalException("年龄非法 age 不能低于1岁不能高于200岁");
        }else {
            System.out.println("年龄合法");
            System.out.println("保存年龄:" + age);
        }
    }
}

3.2自定义运行时异常

public class ItheimaAgeIllegalRuntimeException extends RuntimeException{
    public ItheimaAgeIllegalRuntimeException() {
    }

    public ItheimaAgeIllegalRuntimeException(String message) {
        super(message);
    }
}
public class ExceptionDemo4 {
    public static void main(String[] args) {
        // 目标:认识自定义异常-运行时异常
        System.out.println("程序开始。。。。");
        saveAge(300);
        System.out.println("失败了");
        System.out.println("程序结束。。。。");
    }

    // 需求:我们公司的系统只要收到了年龄小于1岁或者大于200岁就是一个年龄非法异常。
    public static void saveAge(int age) {
        if (age < 1 || age > 200) {
            // 年龄非法;抛出去一个异常。
            throw new ItheimaAgeIllegalRuntimeException("年龄非法 age 不能低于1岁不能高于200岁");
        } else {
            System.out.println("年龄合法");
            System.out.println("保存年龄:" + age);
        }
    }
}

综上两个自定义异常,现在java也在逐渐抛弃编译时异常,像个烫手的山芋.现在开发建议用运行时异常

4.异常的处理方案

public class ExceptionDemo6 {
    public static void main(String[] args) {
        // 目标:掌握异常的处理方案2:捕获异常对象,尝试重新修复。
        // 接收用户的一个定价
        System.out.println("程序开始。。。。");

        while (true) {
            try {
                double price = userInputPrice();
                System.out.println("用户成功设置了商品定价:" + price);
                break;
            } catch (Exception e) {
                System.out.println("您输入的数据是瞎搞的,请不要瞎输入价格!");
            }
        }

        System.out.println("程序结束。。。。");
    }

    public static double userInputPrice(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请您输入商品定价:");
        double price = sc.nextDouble();
        return price;
    }
}

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值