java中异常及异常处理

来源:互联网 发布:脉冲爆震发动机 知乎 编辑:程序博客网 时间:2024/05/17 22:28

一、异常的定义

异常是程序中的一些错误。

二、异常的分类

throwable 是所有异常的父类
-throwable
–>Error
是指程序运行期间,出现十分严重、不可恢复的错误,这时候程序就会终止运行
–>Exception
–>RuntimeException
运行时异常,不会引起编译错误,不需要强制处理
例如:
ClassCastException
NullPointerException
ArrayIndexOutOfBoundsException
ArithmeticException
–>CheckedException
checkedException会引起编译错误,需要强制处理
例如:
IOException
sqlException

三、异常的处理

异常处理有两种:
1、通过try-catch-finally的操作来处理
try:需要被处理的异常快
catch:处理异常的代码块
finally:不管出现或者不出现异常都会执行的代码
catch和finally是可以不用写的,try是必须写的
一个处理异常的try-catch中可以有多个catch(可以根据异常的个数catch多个),也可以catch它们的父类及 Exception。如果是多个异常这时就先catch底层的异常,然后catch它们其中的父类。
同级异常没有先后

public class Test {    public static void main(String[] args) {        int a=10;        String b=null;        try {            a++;            System.out.println(a);            //System.exit(0);加这个finally快中就不会执行            System.out.println(10/0);            System.out.println(b.length());            throw new IOException();        } catch (NullPointerException e) {            System.out.println("处理异常");        }catch  (ArithmeticException e){            System.out.println("处理异常2");        }catch(IOException e){            System.out.println("处理异常4");        }catch(RuntimeException e){            System.out.println("处理异常3");        }finally{            System.out.println("nihao;"+a);        }    }

当try中或者finally中有异常也要处理

异常中finally和return
-是否 出现异常都会执行finally
- 是否在正常代码和异常处理代码中return,仍然会先执行finally再return
-不会执行finally的情况:System.exit(0);

2、通过throws关键字将异常向调用它的类抛出,可以一级一级的抛出直到可以处理了
throw与throws的区别

–throw是语句抛出一个异常。
–throws是方法可能抛出异常的声明。
–throw语句用在方法体内,表示抛出异常,由方法体内的语句处理。
–throws语句用在方法声明后面,表示再抛出异常,由该方法的调用者来处理。

import java.io.IOException;public class Test3 {    public static void main(String[] args) {        try {            sha();        } catch (IOException e) {            e.printStackTrace();        }    }    public static void sha() throws IOException{        throw new IOException();    }    public static void sha2(){        try {            throw new IOException();        } catch (IOException e) {            e.printStackTrace();        }    }}

当throw 一个RuntimeException时,并不会要求强制处理,
当throw 一个CheckedException时,要处理,要么用try-catch-finally,要么在方法声明上加throws 异常名称。

public class Tests {    public static void main(String[] args) {        test();    }    public static void test(){        try {            throw new IOException();        } catch (IOException e) {            e.printStackTrace();        }finally{            System.out.println("finlly快");        }    }    public static void test2(){        throw new NullPointerException();    }    public static void test1() throws IOException{        throw new IOException();    }}

三、自定义异常

通过继承Exception并且通过父类的构造器实现自己的构造方法
来自定义一个异常
自定义异常可以解决exception中没有的异常情况

public class MyException extends Exception {    private static final long serialVersionUID = 1L;    public MyException() {        super();    }    public MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {        super(message, cause, enableSuppression, writableStackTrace);    }    public MyException(String message, Throwable cause) {        super(message, cause);    }    public MyException(String message) {        super(message);    }    public MyException(Throwable cause) {        super(cause);    }}