JAVA中存在的异常

        异常:就是程序运行时候出现不正常情况。

        异常的由来:Java是纯面向对象语言,异常就是Java用面向对象的思想将不正常的情况进行了封装。

        如果访问了数组不存在的下标,java就把这个错误信息封装到ArrayIndexOutOfBoundsException这个类里面。

        异常的分类:

        Error:非常严重的错误。

        一般很少见,也很难通过程序解决。它可能源于程序的bug,但一般更可能源于环境问题,如内存耗尽。错误在程序中无须处理,而有运行环境处理。

        Exception:并不是非常严重的错误。

        编译时异常(非运行时异常,你在写代码时候就需要处理的异常): 程序正确,但因为外在的环境条件不满足引发。例如:这不是程序本身的逻辑错误,Java编译器强制要求必须处理这类异常,如果不去处理这个异常,程序就会报错。(读取一个硬盘上图片,读取的代码逻辑是正确的,但是这个图片所在的硬盘可能出现坏道,就没法读取这个文件,未雨绸缪提前检测可能会有读取硬盘问题)

        运行期异常(RuntimeException,代码运行时候抛出的异常 ): 这意味着程序存在bug,如数组越界,0被除,空指针....这类异常需要更改程序来避免,Java编译器强制要求处理这类异。如果抛出运行时异常,是要根据抛出的异常的信息去分析程序的错误,在写代码时候绝大部分情况下不去try-catch运行时异常。

        自定义异常:

        继承Exception都是编译时异常。

        继承自RuntimeException运行时异常

        在Java中,处理异常的方法主要有以下几种:

  1. try-catch块:使用try-catch块可以捕获并处理异常。在try块中编写可能抛出异常的代码,在catch块中处理捕获到的异常
javaCopy Codetry {
    // 可能会抛出异常的代码
} catch (ExceptionType e) {
    // 处理异常的代码
}

        2.throws关键字:在方法声明中使用throws关键字声明方法可能会抛出的异常,将真正的异常处理交给调用该方法的地方处理

javaCopy Codepublic void someMethod() throws SomeException {
    // 可能会抛出SomeException异常的代码
}

        3. finally块:无论是否发生异常,finally块中的代码都会被执行。通常用于释放资源或进行清理操作

javaCopy Codetry {
    // 可能会抛出异常的代码
} catch (Exception e) {
    // 处理异常的代码
} finally {
    // 无论是否发生异常都会执行的代码
}

        4.自定义异常:通过创建自定义异常类来表示特定的异常情况

javaCopy Codeclass CustomException extends Exception {
    // 构造方法等
}

        5.异常链:通过在catch块中抛出新的异常来保留原始异常信息,并传递更高层次的异常

javaCopy Codetry {
    // 可能会抛出异常的代码
} catch (Exception e) {
    throw new CustomException("Something went wrong", e);
}

一、运行时的异常

1、数组下标异常

        ava.lang.Array Index OutOf Bounds Exception: 4

@Test
public void test2() {
    int[] array = {3,5,7,9};
    for (int i = 0; i <= array.length; i++){
        //ava.lang.Array Index OutOf Bounds Exception: 4
        //数组下标存在边界异常
        System.out.println(array[i]);
    }
}
@Test
public void test2() {
    int[] array = {3,5,7,9};
    for (int i = 0; i < array.length; i++){
        System.out.println(array[i]);
    }
}

2、空指针异常

        NullPointerException空指针异常(引用数据类型)

        修改前:

@Test
public void test4(){
    //在方法里面定义的叫局部变量
    //Student student;在全局里面可以用局部需要初始化
    Student student = null;
    //Variable 'student' might not have been initialized
    //没有初始化
    System.out.println(student);//方法为空正常
    //java.lang.NullPointerException: Cannot read field "id" because "student" is null
    System.out.println(student.id);//属性为空异常

        修改后:

@Test
public void test5(){
    Student student = new Student();
    System.out.println(student.id);
    System.out.println(student.name);
    System.out.println(student.gender);
}

3、字符串数组下标越界异常

        java.lang.StringIndexOutOfBoundsException: String index out of range: 5

public static void main(String[] args) {
    String str = "hello";
    char ch = str.charAt(5);
    System.out.println(ch);
}

二、异常的处理

try {
     需要检查的代码(可能会抛出异常,也可能不会抛出异常)
} catch(异常的类型   异常类型的变量) {
    捕获异常后要处理异常
} finally {
    一定会被执行的代码(不管异常抛不抛出都会执行)
}

@Test
public void test2() {
    //运行时异常是程序员自己写的代码有问题
    //java.lang.ArithmeticException: / by zero
    //int num = 1 / 0;

    String str = null;
    System.out.println(str.toUpperCase());

    //编译时异常:未雨绸缪
    //Unhandled exception: java.io.FileNotFoundException
    try {
        // 需要检查的代码(可能会抛出异常,也可能不会抛出异常)
        FileInputStream fileInputStream = new FileInputStream("a.txt");
        System.out.println("ExceptionDemo.test2 try");
    } catch (FileNotFoundException e) {
        //捕获异常后要处理异常
        e.printStackTrace();
    } finally {
        //一定会被执行的代码(不管异常抛不抛出都会执行)
        System.out.println("finally");
    }
    System.out.println("ExceptionDemo.test2");
}

三、编译时异常

        非运行时异常、检查时异常

        处理的方法有两种:

        1、try-catch捕获这个异常,自己处理了这个异常

        2、throws抛出异常,我不处理这个异常,抛出异常

@Test
public void test31() {
    //Unhandled exception: java.lang.ClassNotFoundException
    // try-catch自己处理这个异常
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

@Test
public void test55()  {
    try {
        show();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

//抛出编译时异常,谁调用我谁去处理
public void show() throws ClassNotFoundException {
    Class.forName("com.mysql.jdbc.Driver");
}

四、多重捕获块(多个catch)

try {
    需要检测的代码(可能会抛出异常,也可能不会抛出异常)
} catch (异常的类型1 异常类型的变量1) {
     捕获异常后处理异常
} catch (异常的类型2 异常类型的变量2) {
     捕获异常后处理异常
} catch (异常的类型3 异常类型的变量3) {
     捕获异常后处理异常
} finally {
     一定会被执行的代码(不管异常抛不抛出都会执行,例如数据库释放连接)
}

@Test
public void test4() {
    try {
        FileInputStream fileInputStream = new FileInputStream("a.txt");
        fileInputStream.read();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        System.out.println("finally");
    }
}

五、自定义异常

        1、所有的异常的都是Throwable的子类

        2、如果写一个运行时异常,需要继承RuntimeException

        3、如果要写一个编译时异常,继承Exception

        案例:模拟银行转账,可以实现存钱和取钱的功能

        取钱时候如果余额不够就抛出异常 MeiQianException

public class MeiQianException extends Exception{

    //message代表抛出异常之后打印的异常信息
    public MeiQianException(String message) {
        super(message);//new Exception(message)
    }

    public void printStackTrace() {
    }
}


@Test
public void test6() {
    try{
        quQian(900);
    } catch (MeiQianException e) {
        e.printStackTrace();
    }
}

public void quQian(double money) throws MeiQianException{
    if (money >= 1000) {
        throw new MeiQianException("钱不够");
    }
    System.out.println("钱够了");
}

六、throws、throw

        throws是在方法签名上

        throw是在代码中抛出异常

public void quQian(double money) throws MeiQianException{
    if (money >= 1000) {
        throw new MeiQianException("钱不够");
    }
    System.out.println("钱够了");
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值