Skip to content

java exception

yaokun123 edited this page Nov 21, 2018 · 6 revisions

JAVA面向对象-异常

一、异常概述

  • 异常的体系
Throwable
    ->Error
        通常出现重大问题如,运行的类不存在或者内存溢出等。
        不编写针对代码对其处理。
    ->Exceptiom
        在运行时运行出现的一些情况,可以通过try catch finally
  • Exception和Error的子类名都是以父类名作为后缀。

二、异常体系

用异常类对其进行描述

不同的问题用不同的类进行具体的描述。比如角标越界,空指针等等。

问题很多,意味着描述的类也很多

将其共性进行向上抽取,形成了异常体系

最总问题(不正常情况)就分成了两大类.

Throwable:无论是error还是异常,问题,问题发生就应该可以抛出,让调用者知道并处理。
         //该体系的特点就在于Throwable及其所有的子类都具有可抛性。
         //可抛性到底指的是什么呢?怎么体现可抛性呢?
         //其实是通过两个关键字来实现的。
         //throws throw,凡是可以被这两个关键字所操作的类和对象都具备可抛性。
    |--1、一般不可处理的Error
         //特点:是由JVM抛出的严重性问题。这种问题发生一般不针对性处理。直接修改程序
    |--2、可以处理的Exception

该体系的特点

子类的后缀名都是用其父类名作为后缀,阅读性很强。

数组角标越界示例

class ExceptionDemo{
    public static void main(String[] args){
        int[] arr = new int[3];
        //System.out.println(arr[3]);//数组越界

        Demo d = new Demo();
        d.method(arr,3);
    }
}

class Demo{
    public static void method(int[] arr,int index){

        if(index>=arr.length){//不用JVM自带的抛出异常机制,自己抛出异常
            throw new ArrayIndexOutOfBoundsException("数组角标越界了:"+index);
        }

        if(index<0){//不用JVM自带的抛出异常机制,自己抛出异常
            throw new ArrayIndexOutOfBoundsException("数组角标不能为负数:"+index);
        }
        System.out.println(arr[index]);
    }
}

三、自定义异常类

对于角标是整数不存在,可以用角标越界表示。 对于负数为角标的情况,准备用负数角标异常来表示。

负数角标这种异常在java中并没有定义过。 那就按照java异常的创建思想,面向对象,将负数角标进行自定义描述,并封装成对象。 这种自定义的问题描述称为自定义异常

注意:如果让一个类成为异常类,必须要继承异常体系,因为只有成为异常体系的子类才有资格具备可抛性。 才可以被两个关键字所操作。(throw,throws)

class FuShuIndexException extends Exception{
    FuShuIndexException(){}//空参数构造函数
    
    FuShuIndexException(String msg){//字符串参数构造函数
        super(msg);//直接使用父类的构造方法
    }
}

class ExceptionDemo{
    public static void main(String[] args)throws FuShuIndexException{//声明抛出
        int[] arr = new int[3];
        //System.out.println(arr[3]);//数组越界

        Demo d = new Demo();
        d.method(arr,3);
    }
}

class Demo{
    public static void method(int[] arr,int index)throws FuShuIndexException{//声明抛出

        if(index>=arr.length){//不用JVM自带的抛出异常机制,自己抛出异常
            throw new ArrayIndexOutOfBoundsException("数组角标越界了:"+index);
        }

        if(index<0){//不用JVM自带的抛出异常机制,自己抛出异常--使用自定义异常类
            throw new FuShuIndexException("角标变成负数了!");
        }
        System.out.println(arr[index]);
    }
}

编译时检测异常和运行时异常的区别(throw&throws)

异常分类:

1、编译时被检测异常:只要是Exception和其子类都是,除了特殊子类RuntimeException体系 这种问题一旦出现,希望在编译时就进行检测,让这种问题有对应的处理方式。

2、编译时不检测异常(运行时异常):就是Exception中的RuntimeException体系和其子类 这种问题的发生,无法让功能继续,运算无法进行,更多是调用者的原因导致的或者引发了内部状态的改变导致的。 那么这种问题一般不处理,直接编译通过,在运行时,让调用者调用时的程序强制停止,让调用者对代码进行修改。

class FuShuIndexException extends RuntimeException{//继承运行时异常体系,就不需要throws
    FuShuIndexException(){}//空参数构造函数
    
    FuShuIndexException(String msg){//字符串参数构造函数
        super(msg);//直接使用父类的构造方法
    }
}

所以自定义异常时,要么继承Exception要么继承RuntimeException

throw和throws区别

1、throws使用在函数上
throw使用在函数内

2、throws抛出的是异常类,可以抛多个,用逗号隔开
throw抛出的是异常对象

四、异常捕捉try...catch...finally

异常处理的捕捉形式

这是可以对异常进行针对性处理的方式。

具体格式是:

try{
    //需要被检测异常的代码
}catch(异常类 变量){//该变量用于接受发生的异常对象
    //处理异常的代码
}finally{
    //一定会被执行的代码
}

代码示例

class FuShuIndexException extends Exception{
    FuShuIndexException(){}//空参数构造函数
    
    FuShuIndexException(String msg){//字符串参数构造函数
        super(msg);//直接使用父类的构造方法
    }
}

class ExceptionDemo{
    public static void main(String[] args){/
        int[] arr = new int[3];
        //System.out.println(arr[3]);//数组越界

        Demo d = new Demo();
        try{
            int num = d.method(arr,-30);
            System.out.println(num);
        }catch(FuShuIndexException e){
            System.out.println(e.getMessage());
            System.out.println(e.toString());
            e.printStackTrace();//JVM的默认异常处理机制就是调用异常对象的这个方法
            System.out.println("负数角标异常");
        }
        System.out.println("over");
    }
}

class Demo{
    public static void method(int[] arr,int index)throws FuShuIndexException{//声明抛出

        if(index<0){//不用JVM自带的抛出异常机制,自己抛出异常--使用自定义异常类
            throw new FuShuIndexException("角标变成负数了!");
        }
        System.out.println(arr[index]);
    }
}

finally代码块

class Demo{
    public void show(int index){
        if(index<0){
            throw new ArrayIndexOutOfBoundsException("越界了");
        }
        int[] arr = new int[3];
        return arr[index];
    }
}

class ExceptionDemo{
    public static void main(String[] args){
        Demo d = new Demo();
        try{
            int num = d.show(-3)
        }catch(ArrayIndexOutOfBoundsException e){
            System.out.println(e.toString());
            return;
        }finally{//finally代码块一定会被执行(除了在上面调用System.exit(0)推出JVM)
            System.out.println("finally");
        }
        
        System.out.println("over");
    }
}
Clone this wiki locally