8.8Java 如何抛出异常、自定义异常

一、异常的抛出

1、定义 : 一个方法不处理这个异常,而是调用层次向上传递,谁调用这个方法,这个异常就由谁来处理。

2、throw : 将产生的异常抛出(强调的是动作),抛出的既可以是异常的引用,也可以是异常对象。(位置: 方法体内

3、throws : 如果一个方法可能会出现异常,但没有能力处理这种异常,可以在方法声明处用throws子句来声明抛出异常。用它修饰的方法向调用者表明该方法可能会抛出异常(可以是一种类型,也可以是多种类型,用逗号隔开)(位置:写在方法名 或方法名列表之后 ,在方法体之前。)

注意 : 调用可能会抛出异常的方法,必须添加try-catch代码块尝试去捕获异常 或者 添加throws 声明 来将异常 抛出给更上一层的调用者进行处理,这里需要注意一个细节:新的异常包含原始异常的所有信息,根据这个我们可以去追溯最初异常发生的位置

如下图所示 
这里写图片描述

4、简单使用

// 定义一个方法,抛出 数组越界和算术异常(多个异常 用 "," 隔开)
    public void Test1(int x) throws ArrayIndexOutOfBoundsException,ArithmeticException{

    System.out.println(x);

    if(x == 0){

        System.out.println("没有异常");
        return;
    }

    //数据越界异常
    else if (x == 1){

        int[] a = new int[3];
         a[3] = 5;
    }

    //算术异常
    else if (x == 2){

        int i = 0;
        int j = 5/0;
    }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

在main方法中调用

public static void main(String[] args) {

        //创建对象
        ExceptionInital object = new ExceptionInital();

        // 调用会抛出异常的方法,用try-catch块
        try{

            object.Test1(0);

        }catch(Exception e){

            System.out.println(e);
        }

        // 数组越界异常
        try{

            object.Test1(1);
        }catch (ArrayIndexOutOfBoundsException e) {

            System.out.println("数组越界异常:"+e);
        }


        // 算术异常
        try{

            object.Test1(2);

        }catch(ArithmeticException e){

            System.out.println("算术异常:"+e);
        }


        //使用 throw 抛出异常(可以抛出异常对象,也可以抛出异常对象的引用)
        try{

            ArrayIndexOutOfBoundsException  exception = new ArrayIndexOutOfBoundsException();

            throw exception;//new ArrayIndexOutOfBoundsException();

        }catch(ArrayIndexOutOfBoundsException e){

            System.out.println("thorw抛出异常:"+e);
        }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

运行结果 
这里写图片描述

总结下 throw 和throws 关键字的区别

1、写法上 : throw 在方法体内使用,throws 函数名后或者参数列表后方法体前 
2、意义 : throw 强调动作,而throws 表示一种倾向、可能但不一定实际发生 
3、throws 后面跟的是异常类,可以一个,可以多个,多个用逗号隔开。throw 后跟的是异常对象,或者异常对象的引用。 
4、throws 用户抛出异常,当在当前方法中抛出异常后,当前方法执行结束(throws 后,如果有finally语句的话,会执行到finally语句后再结束。)。可以理解成return一样。

二、自定义异常

前面所讲的异常,都是系统自带的,系统自己处理,但是很多时候项目会出现特有问题,而这些问题并未被java所描述并封装成对象,所以对于这些特有的问题可以按照java的对问题封装的思想,将特有的问题进行自定义异常封装。在Java中要想创建自定义异常,需要继承Throwable或者他的子类Exception。

语法

class  自定义异常类 extends 异常类型(Exception){

 // 因为父类已经把异常信息的操作都完成了,所在子类只要在构造时,将异常信息传递给父类通过super 语句即可。
  // 重写 有参 和 无参  构造方法
}
  • 1
  • 2
  • 3
  • 4
  • 5

例如:

public class CustomException extends Exception {

    //无参构造方法
    public CustomException(){

        super();
    }

    //有参的构造方法
    public CustomException(String message){
        super(message);

    }

    // 用指定的详细信息和原因构造一个新的异常
    public CustomException(String message, Throwable cause){

        super(message,cause);
    }

    //用指定原因构造一个新的异常
     public CustomException(Throwable cause) {

         super(cause);
     }

}

// 备注: 这些方法怎么来的? 重写父类Exception的方法,那么如何查看Exception具有哪些API,快捷键:选中Exception, command+单击。windows系统 :选中Exception, control+单击。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

自定义异常的使用例子: 
自定义test1()方法,抛出 “我喝酒了”的异常信息,test2()方法调用test1()方法,并将异常包装成RuntimeException类型的异常,继续抛出,在main方法中调用test2()方法,并尝试捕获异常

public void test2() {

        try{

            test1();

        }catch (CustomException e){

           RuntimeException exception  =  new RuntimeException(e);
           //exception.initCause(cause)
           throw  exception;
        }

    }

    public void test1() throws CustomException{

        throw new CustomException("我喝酒了");
    }
// main方法
    public static void main(String[] args) {

        CustomExceptionInital object =  new  CustomExceptionInital();

        //try{

            object.test2();

        //}catch(Exception e){

        //e.printStackTrace();

        //}

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

输出结果: 
这里写图片描述

思考 ? 为什么上述demo, test1() 方法 抛出异常了,但是test1() 方法自己没办法处理,所以在 参数列表后方法体前将该异常抛出了,test2() 方法调用了test1()方法捕获其异常,并将其异常 包装成 RuntimeException 异常继续抛出,但是test2()方法却没有声明 抛出异常 ? 而且,在main 方法中,调用test2()方法的时候,也不用try-catch 代码块去捕获 异常呢 ?点击我告诉你为什么

猜你喜欢

转载自blog.csdn.net/qq_18048847/article/details/80450626