异常

异常

1. 异常体系结构

public class Test {
    public static void func() {
        func();
    }

    // 错误
    public static void main2(String[] args) {
        func();
    }

    // 运行时异常
    public static void main1(String[] args) {
        System.out.println(10/0);
        System.out.println("abc");
    }
}
class Person implements Cloneable {
    public String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException{
        // 一定得在子类方法中, 调用protected修饰的父类成员
        return super.clone();
    }
}

public class Test {

    public static void main(String[] args) /*throws CloneNotSupportedException*/ {
        Person person1 = new Person("a",1);

        // 报错 ! 编译时异常
        Person p2 = (Person) person1.clone();
        System.out.println(p2.name);
    }
}

 

2. throw 抛出异常 / throws 声明异常

我们自己用throw 关键字 抛出的异常。一般情况下,用throw抛出自定义异常

public class Test {
    public static void func() {
        // 自己手动抛出异常
        throw new NullPointerException("test");
        
    }

    public static void main(String[] args) {
        func();
    }
}

throw 细节:

  1. throw必须写在方法体内部
  2. 抛出的对象必须是Exception 或者 Exception 的子类对象
  3. 如果抛出的是 RunTimeException 或者 RunTimeException 的子类,则可以不用处理,直接交给JVM来处理
  4. 如果抛出的是编译时异常,用户必须处理,否则无法通过编译
  5. 异常一旦抛出,其后的代码就不会执行

 

throws  声明异常

throws必须跟在方法的参数列表之后

class Person implements Cloneable {
    public String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException{
        // 一定得在子类方法中, 调用protected修饰的父类成员
        return super.clone();
    }
}

public class Test {

    // throws 只是声明异常 并不处理异常, 在这里例子中 throws CloneNotSupportedException
    // 告诉main函数的调用者, 在main方法内部有个 CloneNotSupportedException异常, 需要去处理
    // 如果程序的异常没有被程序员处理, 那么这个异常就会交给 JVM 处理, 程序立即停止执行
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person("a",1);

        // 报错 ! 编译时异常
        Person p2 = (Person) person1.clone();
        System.out.println(p2.name);
    }
}

 

3. 处理异常

try - catch

public class Test {
    public static void func() throws NullPointerException {
        throw new NullPointerException();
    }

    public static void main(String[] args) {
        try {
            func();
        }catch (NullPointerException e) {
            // 打印异常信息
            e.printStackTrace();
            System.out.println("捕获到空指针异常");
        }
    }
}

 

finally

用来对资源进行回收

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        try {
            int a = scanner.nextInt();
            int[] arr = {1,2,3};
            System.out.println(arr[3]);
        }catch (NullPointerException e){
            System.out.println("捕捉空指针异常");
        }catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("捕捉数组越界异常");
        } finally {
            // 释放资源
            System.out.println("执行finally");
            scanner.close();
        }
    }
}

 

finally在最后一定会被执行

import java.util.InputMismatchException;
import java.util.Scanner;

public class Test {
    public static int func2() {
        Scanner scanner = new Scanner(System.in);
        try {
            int a = scanner.nextInt();
            return a;
        }catch (InputMismatchException e) {
            e.printStackTrace();
            System.out.println("捕获InputMismatchException异常.....");
        }finally {
            System.out.println("finally被执行了...");
            scanner.close();
            return 99;
        }
    }

    public static void main(String[] args) {
        System.out.println(func2());
    }
}

 

posted @ 2024-03-29 10:49  qyx1  阅读(38)  评论(0)    收藏  举报