Java异常

异常

在这里插入图片描述

  • Exception:叫做异常,代表程序可能出现的问题。我们通常会用Exception以及它的子类来封装程序出现的问题。
  • 运行时异常:RuntimeException及其子类,编译阶段不会出现异常提醒。运行时出现异常(如:数组索引越界异常)
  • 编译时异常:编译阶段就会出现异常提醒的。(如:日期解析异常)
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExceptionDemo1 {
    public static void main(String[] args) throws ParseException {
        //编译时异常(在编译阶段,必须要手动处理,否则代码报错)
        String time = "2030年1月1日";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = sdf.parse(time);
        System.out.println(date);
        //运行时异常
//        int[] arr = {1,2,3,4,5};
//        System.out.println(arr[10]);
    }
}
  • 异常的作用:
    1、异常是用来查询bug的关键参考信息
    2、异常可以作为方法内部的一种特殊返回值,以便通知调用者底层的执行情况

  • 异常的处理方式
    1、JVM默认的处理方式
    2、自己处理
    3、抛出异常

捕获异常

- 格式:
try{
可能出现异常的代码;
}catch(异常类名 变量名){
异常处理的代码;
}
目的:当代码出现异常时,可以让程序继续往下执行。

public class ExceptionDemo2 {
    public static void main(String[] args){
        int[] arr = {1,2,3,4,5};
        try{
            //可能出现异常的代码
            System.out.println(arr[10]);
        }catch (ArrayIndexOutOfBoundsException e){
            /*
            此处出现了异常,程序会在这里创建一个ArrayIndexOutOfBoundsException对象
            new ArrayIndexOutOfBoundsException()
            拿着这个对象到catch的小括号中对比,看括号中的变量是否可以接收这个对象
            如果能被接收,就表示该异常就被捕获(抓住),执行catch里面对应的代码
            当catch里面所有的代码执行完毕,继续执行try...catch体系下面的其他代码
             */
            //如果出现了ArrayIndexOutOfBoundsException异常,该如何处理
            System.out.println("索引越界了");
        }
        System.out.println("看看我执行了吗?");
    }
}
  • 问题1:如果try中没有遇到问题,怎么执行?
    答:会把try里面所有的代码全部执行完毕,不会执行catch里面的代码
  • 问题2:如果try中可能会遇到多个问题,怎么执行?
    答:会写多个catch与之对应,父类异常需要写在下面
  • 问题3:如果try中遇到的问题没有被捕获,怎么执行?
    答:相当于try…catch白写了。当前异常会交给虚拟机处理
  • 问题4:如果try中遇到问题,那么try下面的其它代码还会执行吗?
    答:不会执行了。try中遇到问题,直接跳转到对应的catch,如果没有对应的catch与之匹配。则交给虚拟机处理。

异常中常用的方法

在这里插入图片描述

public class ExceptionDemo3 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        try {
            System.out.println(arr[10]);
        } catch (ArrayIndexOutOfBoundsException e) {
//            String message = e.getMessage();
//            System.out.println(message);

//            String string = e.toString();
//            System.out.println(string);

            e.printStackTrace();//仅仅是打印信息,不会停止程序运行
        }
        System.out.println("看看我执行了吗?");
    }
}

抛出处理

  • throws:
    注意:写在方法定义处,表示声明一个异常,告诉调用者,使用本方法可能会有哪些异常
    编译时异常:必须要写
    运行时异常:可以不写
  • throw
    注意:写在方法内,结束方法,手动抛出异常对象,交给调用者方法中下面的代码不再执行了

需求:求数组中的最大数字

public class ExceptionDemo4 {
    public static void main(String[] args) {
        int[] arr = {1,2,3};
        int max = 0;
        try {
            max = getMax(arr);
        } catch (NullPointerException e) {
            System.out.println("空指针异常");
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("索引越界异常");
        }
        System.out.println(max);
    }
    public static int getMax(int[] arr)/*throws NullPointerException,ArrayIndexOutOfBoundsException*/{
        if(arr == null){
            throw new NullPointerException();
        }
        if(arr.length == 0){
            throw new ArrayIndexOutOfBoundsException();
        }
        System.out.println("看看我执行了吗");
        int max = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] > max){
                max = arr[i];
            }
        }
        return max;
    }
}

自定义异常

步骤:
1、定义异常类
2、写继承关系
3、空参构造
4、带参构造
意义:就是为了让控制台的报错信息更加的见名知意

public class NameFormatException extends RuntimeException{
    //技巧:
    //NameFormat:当前异常的名字,表示姓名格式化问题
    //Exception:表示当前类是一个异常类
    
    //运行时:RuntimeException 核心:就表示由于参数错误而导致的问题
    //编译时:Exception 核心:提醒程序员检查本地信息
    public NameFormatException(){
        
    }
    public NameFormatException(String message){
        super(message);
    }
}
public class AgeOutofBoundsException extends RuntimeException{
   public AgeOutofBoundsException(){

   }
   public AgeOutofBoundsException(String message){
       super(message);
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值