Java异常

一、什么是异常

在这里插入图片描述

public class ExceptionTest1 {
    public static void main(String[] args) throws ParseException{
        //Integer.valueOf("abc");

//        int[] arr = {1,2,3};
//        System.out.println(arr[5]);

       /* try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d=sdf.parse("2028-11-11 10:24:54");
            System.out.println(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }*/
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d=sdf.parse("2028-11-11 10:24");
        System.out.println(d);
    }
}

二、异常的体系

在这里插入图片描述

三、抛出异常

在这里插入图片描述

四、自定义异常

在这里插入图片描述
AgeIllegalException

//必须让这个类继承自Exception,才能成为一个编译时异常类。
public class AgeIllegalException extends Exception{
    public AgeIllegalException() {

	}
    public AgeIllegalException(String message) {
		super(message);
	}

}

AgeIllegalRuntimeException

//必须让这个类继承自RuntimeException,才能成为一个运行时异常类。
public class AgeIllegalRuntimeException extends RuntimeException{
    public AgeIllegalRuntimeException() {

	}
    public AgeIllegalRuntimeException(String message) {
		super(message);
	}

}

public class ExceptionTest2 {
    public static void main(String[] args) {
        // 需求:保存一个合法的年龄
        try {
            saveAge(130);
            System.out.println("底层执行成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("底层出现了bug");
        }
        try {
            saveAge2(100);
            System.out.println("saveAge2底层执行成功");
        } catch (AgeIllegalException e) {
            e.printStackTrace();
            System.out.println("saveAge2底层出现了bug");
        }
    }

   //运行时异常
    public static void saveAge(int age) {
        if (age > 0 && age < 120) {
            System.out.println("年龄被成功保存:" + age);
        } else {
            // 用一个异常对象封装这个问题
            throw new AgeIllegalRuntimeException("/age is illegal,your age is " + age);
        }
    }

    //编译时异常 throws用在方法上,抛出方法内部的异常
    public static void saveAge2(int age) throws AgeIllegalException {
        if (age > 0 && age < 120) {
            System.out.println("年龄被成功保存:" + age);
        } else {
            // 用一个异常对象封装这个问题
            throw new AgeIllegalException("/age is illegal,your age is " + age);
        }
    }
}

五、异常的作用

在这里插入图片描述

public class ExceptionTest3 {
    public static void main(String[] args) {
        try {
            test1();
        } catch (ParseException e) {
            System.out.println("解析的时间有问题");
            e.printStackTrace();//打印出这个异常信息记录下来
        } catch (FileNotFoundException e) {
            System.out.println("要找的文件不存在");
            e.printStackTrace();
        }
    }
    public static void test1() throws ParseException, FileNotFoundException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d=sdf.parse("2028-11-11 10:24");
        System.out.println(d);
        test2();
    }
    public static void test2() throws FileNotFoundException {
        //读取文件
        InputStream is=new FileInputStream("d:/test.txt");
    }
}

public class ExceptionTest3_2 {
    public static void main(String[] args) {
        try {
            test1();
        } catch (Exception e) {
            System.out.println("您当前的操作有问题");
            e.printStackTrace();//打印出这个异常信息记录下来
        }
    }
    public static void test1() throws Exception {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d=sdf.parse("2028-11-11 10:24");
        System.out.println(d);
        test2();
    }
    public static void test2() throws Exception {
        //读取文件
        InputStream is=new FileInputStream("d:/test.txt");
    }
}

六、异常常见处理方式

在这里插入图片描述
在这里插入图片描述

/**
 * 掌握异常的处理方式:捕获异常,尝试修复
 */
public class ExceptionTest4 {
    public static void main(String[] args) {
        //需求:调用一个方法,让用户输入一个合适的价格返回为止
        //思路:使用循环,捕获异常,提示用户重新输入尝试修复
        while (true) {
            try {
                System.out.println("合适的价格是:"+getPrice());
                break;
            } catch (Exception e) {
                System.out.println("请输入合法的数字!!!!");
            }
        }
    }

    public static double getPrice(){
        Scanner sc=new Scanner(System.in);
        while (true) {
            System.out.println("请输入合适的价格");
            double price=sc.nextDouble();
            if(price>=0){
               return price;
            }else{
                System.out.println("您输入的价格是不合适的");
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值