异常

异常

Java的异常处理机制:

在java程序运行过程中发生的一些问题,称为异常。Java异常处理机制会根据发生的问题的类型,将其封装成一个异常对象,然后抛出。

Java异常处理机制的Api的继承关系图:

在继承关系图中可知,java的异常处理机制的顶层父类是Throwable,

Throwable派生两个子类:

1.  Error错误

指程序在运行期间发生了某种错误(XxxError),Error 错误通常没有具体的处 理方式,程序将会结束运行。   

Error 错误的发生往往都是系统级别的问题,都是 jvm 所在系统发生的,并反馈给 jvm 的。
我们无法针对处理,只能修正代码。
2.  Exception
指程序在编译、运行期间发生了某种异常(XxxException),我们可以对异常进 行具体的处理。 若不处理异常,
程序将会结束运行

Java中的异常处理机制当中的5个关键字:

try catch finally throw throws

try需要 与catch结合使用,try用来监控可能会导致异常发生的代码,catch是用来处理和捕获异常的,前提条件是try当中发生的异常的类型和catch要捕获的异常的类型要一致。

public class TestException1 {
    public static void main(String[] args) {
        try {
            int[] arrs=new int[10];
            System.out.println(arrs[10]);
        }catch(ArrayIndexOutOfBoundsException ex) {
            System.out.println("数组下标越界......");
        }
        System.out.println("程序继续执行......");
    }
}

当异常发生的时候catch块儿中的代码会执行,没有异常发生,则catch块儿中的代码不会执行。

finally关键字与try块儿或try …catch块儿结合使用,在finally块儿当中放的是无论异常是否发生都要执行的代码块儿。一般是一些资源释放的操作,比如文件流的关闭,或数据库连接关闭等操作。

public class TestException1 {
    public static void main(String[] args) {
        try {
            int[] arrs=new int[10];
            System.out.println(arrs[10]);
        }catch(ArrayIndexOutOfBoundsException ex) {
            System.out.println("数组下标越界......");
        }finally {
            //关文件流
            System.out.println("始终执行");
        }
        System.out.println("程序继续执行......");
    }
}

以下情况,即使用发生异常,执行catch遇到return,也照样会执行finally

public class TestException2 {
    public static void main(String[] args) {
        try {
            int[] arrs=new int[10];
            System.out.println(arrs[10]);       
        }catch(ArrayIndexOutOfBoundsException ex) {
            System.out.println("数组下标越界......");
            return;
        }finally {
            //关文件流
            System.out.println("始终执行");
        }
        System.out.println("程序继续执行......");
    }
}

finally块儿中代码不会执行的第一种情况:发生了异常,执行catch,catch遇取System.exit(0)。

public class TestException2 {
    public static void main(String[] args) {
        try {
            int[] arrs=new int[10];
            System.out.println(arrs[10]);       
        }catch(ArrayIndexOutOfBoundsException ex) {
            System.out.println("数组下标越界......");
            System.exit(0);     
}finally {
            //关文件流
            System.out.println("始终执行");
        }
        System.out.println("程序继续执行......");
    }
}

第二种情况:在try块儿中没有发生异常,使用了System.exit(0)退出应用,此时不会执行finally块儿的代码。

public class TestException2 {
    public static void main(String[] args) {
        try {
            int[] arrs=new int[10];
            System.out.println(arrs[5]);
            System.exit(0);
        }catch(ArrayIndexOutOfBoundsException ex) {
            System.out.println("数组下标越界......");
        }finally {
            //关文件流
            System.out.println("始终执行");
        }
        System.out.println("程序继续执行......");
    }
}

多重catch块儿

如果在try块儿存在多种可能会发生的异常,可以使用多重catch块儿进行捕获,多重catch块儿的异常类型的顺序是:先子类异常,后父类异常。

public class TestException3 {

    public static void main(String[] args) {
        try {
            Scanner input=new Scanner(System.in);
            System.out.println("请输入除数:");
            int n1=input.nextInt();
            System.out.println("请输入被除数:");
            int n2=input.nextInt();
            int r=n1/n2;
            System.out.println(r);

            int[] arrs=new int[5];
            System.out.println(arrs[5]);
        }catch(ArrayIndexOutOfBoundsException ex) {
            System.out.println("数组下标越界");
        }catch(ArithmeticException ex) {
            System.out.println("被0除");
        }catch (Exception e) {
            System.out.println("");
        }finally {
            System.out.println("始终执行......");
        }
        System.out.println("程序结束!");
    }
}

throws关键字(声明该方法可能会导致的异常)

throws关键字是用在方法签名的位置,作用:当方法中存在有可能导致异常的代码,但方法本身又不想或不能做异常的处理,通过该关键字声明方法可能可能会产生的异常,告诉方法的调用者,如果调用这个方法,需要对这个方法可能产生的异常进行处理。

throws后面可以同声明多种异常,异常之间用逗号隔开,形成一个异常的列表。

public class Calculator {
    //此方法可能导致被0除的异常
    public void cal() throws Exception, ArithmeticException,InputMismatchException {
        Scanner input=new Scanner(System.in);
        System.out.println("请输入被除数:");
        int n1=input.nextInt();
        System.out.println("请输入除数:");
        int n2=input.nextInt();
        int r=n1/n2;
        System.out.println("商r="+r);
    }
}

public class TestThrows1 {
    public static void main(String[] args) {
        try {
            Calculator c = new Calculator();
            c.cal();
        } catch (Exception ex) {
            System.out.println("被零除");
        }
    }
}

方法的调用者也可以不用处理异常,把异常继续向上声明抛出,直到抛给jvm。

public class TestCalculator {
    public static void main(String[] args) throws Exception{
        Calculator c=new Calculator();
        c.cal();
    }
}

throw(真正的引发一个异常)

throw用在方法中,真正的引发一个异常,而throws方法在方法的签名处,它的作用是告诉方法的调用者此方法可能会引发的引常的类型。

public static void main(String[] args) {
        try {
            test();
        } catch (Exception e) {
            //System.err.println(e.getMessage());
            e.printStackTrace();
        }
    }

    public static void test() throws Exception{
        Scanner input=new Scanner(System.in);
        System.out.println("请输入一个整数:");
        if(input.hasNextInt()) {
            System.out.println(input.nextInt());
        }else {
            //引发一个异常
            throw new Exception("请输入一个整数");
        }
    }
}

自定义异常:

当系统提供的异常不能满足需求时可以自定义异常,自定义异常的方法就是定义一个类继承Exception父类。

public class PersonException extends Exception{ 
    public PersonException(String message) {
        super(message);
    }
}

public class Person {
    private String name;
    private String gender;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) throws PersonException {
        if(gender.equals("男")||gender.equals("女")) {
            this.gender = gender;
        }else {
            throw new  PersonException("性别只能是男或女");
        }
    }
posted @ 2019-08-08 19:30  流氓的夏天  阅读(166)  评论(0编辑  收藏  举报