19异常的学习笔记

2023-11-16

异常

很重要,有利于我们平时处理问题
异常就是代表程序出现了问题
常见的异常比如说

  1. 数组越界
  2. 除法除0

异常的体系是什么

java.lang.Throwable
Error Exception
RuntimeException 其他异常

Error 代表的是系统级别的错误,也就是一旦系统出现问题,sun公司会把这些问题封装程Error对象出来
Error 是sun公司自己用的,不是给我们程序员用的,我们开发人员不用管
Exception:叫异常,它代表的才是我们程序可能出现的问题,所以,我们程序员通常会用 Exception 以及它的孩子来封装程序出现的问题。
运行时异常:RuntimeException及其子类,编译阶段不会出现错误提醒,运行时出现的异常(如:数组索引越界异常)
编译时异常:编译阶段就会出现错误提醒的。(如:日期解析异常)
抓住异常,我们通过这个代码来进行抓住异常,如果 try 里面的代码是有异常的,那我们就进行捕捉,如果捕捉到,我们就可以得到这个异常信息,并输出这个异常的信息

        try {
            SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d = date.parse("2020-11-5 11:50:90");
            System.out.println(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }

自定义运行时异常

  1. 定义一个异常类继承RuntimeExceptin
  2. 重写构造器
  3. 通过throw new 异常类来创建异常对象并输出

编译阶段不报错,提醒不强烈,运行时才报错

public class exception {
    public static void main(String[] args) {
            saveAge(180);
    }
    public  static  void saveAge(int age){
        if(age>0&&age<150){
            System.out.println("年龄被成功保存"+age);
        }else {
            throw  new AgeIllegalRuntimeException("age is illegal ,your age is "+age);
        }
    }
}

我们这个新建的这个异常类我们是继承这个RuntimeException

public class AgeIllegalRuntimeException extends RuntimeException{
	//构造函数
    public AgeIllegalRuntimeException() {
    }
	//构造函数,其中这个message是输出的异常信息
    public AgeIllegalRuntimeException(String message) {
        super(message);
    }
}

抛出编译时异常

public class exception {
    public static void main(String[] args) {
            saveAge(12);
            //saveAge2(25);   //这里会直接报错,我们有两种解决办法,一种是继续往上抛
            try {
                saveAge2(160);
                System.out.println("程序正常执行");
            } catch (AgeIllegleException e) {
                System.out.println("程序异常");
                throw new RuntimeException(e);
            }

    }
    public  static  void saveAge(int age){
        if(age>0&&age<150){
            System.out.println("年龄被成功保存"+age);
        }else {
            throw  new AgeIllegalRuntimeException("age is illegal ,your age is "+age);
        }
    }
    //throws 是在这个方法中抛出异常,让该方法调用的时候出现
    //throw 跑出去一个异常对象。
    public  static  void saveAge2(int age) throws AgeIllegleException {
        if(age>0&&age<150){
            System.out.println("年龄被成功保存"+age);
        }else {
            throw  new AgeIllegleException("age is illegal ,your age is "+age);
        }
    }
}

分析上面的代码,我们可以看到这些异常的处理的时候我们发现我们可以不断的向外面抛出异常,但是一直抛出异常肯定是不可以的,因此我们需要对其进行捕获异常,利用try catch,进行记录异常,并记录处理信息

异常处理

1. 捕获异常,记录异常并相应合适的信息给用户
2. 捕获异常,尝试重新修复

1. 捕获异常,记录异常并相应合适的信息给用户

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ExceptionNesting {
    public static void main(String[] args) {
        try {
            test1();
        } catch (FileNotFoundException e) {
            System.out.println("文件未找到");
            throw new RuntimeException(e);
        } catch (ParseException e) {
            System.out.println("日期格式 不正确");
            throw new RuntimeException(e);
        }
    }
    public static void test1() throws FileNotFoundException, ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse("2020-10-20 12:12");
        System.out.println(date);
        test2();
    }
    public static void test2() throws FileNotFoundException {
        InputStream is = new FileInputStream("D:/meinv.png");
    }
}

2. 捕获异常并进行处理

import java.util.Scanner;

public class StrongExceptionCorrect {
    public static void main(String[] args) {
        while (true) {
            try {
                System.out.println(getMoney());
                break;
            } catch (Exception e) {
                System.out.println("你输入的价格不合适请重新输入(你输入的并不是一个double类型的变量,可能输入了字符串导致异常)");
            }
        }
    }
    public static double getMoney(){
        Scanner input = new Scanner(System.in);
        while (true){
            System.out.println("请您输入合适的价格");
            double money = input.nextInt();
            if(money>=0){
                return money;
            }else {
                System.out.println("你输入的价格是不合适的");
            }
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

19异常的学习笔记 的相关文章

随机推荐