RuntimeException异常处理汇总
Java中所有异常的⽗类是Throwable类,在Throwable类下有两⼤⼦类:
⼀个是Error类,指系统错误异常,例如:VirtualMachineError 虚拟机错误,ThreadDeath 线程死锁。⼀般如果是Error类的异常的话,就是程序的硬伤,就好⽐是⼯⼚⾥断⽔断电,机器损坏了。
另⼀个是Exception类,指编码、环境、⽤户操作输⼊等异常,这个是⽐较常见的异常类,Exception类下⾯⼜有两个⼦
类,RuntimeException ⾮检查异常和检查异常,⾮检查⼜称为运⾏时异常,在RuntimeException异常中有⼏个常见的⼦类,例如:
InputMismatchException 输⼊不匹配异常
ArithmeticException 算术运算异常
NullPointerException 空指针异常
ArrayIndexOutOfBoundsException 数组下标越界异常
ClassCastException 类型转换异常
检查异常中的⼦类有:
IOException ⽂件异常
SQLException SQL数据库错误异常
在实际的开发中,处理异常⼀般使⽤以下三种⽅式:
⼀、使⽤try-catch语句块捕获和处理异常
使⽤try-catch 以及 try-catch-finally 来捕获和处理异常时,catch⾥的异常列表⼀般是⼦类在前,⽗类在后,不然编译时程序会报错。⽰例如下:
1 import java.util.InputMismatchException;
2 import java.util.Scanner;
3
4 public class 异常处理 {
5
6    public static void main(String[] args) {
7
8        System.out.println("请输⼊你的年龄");
9        Scanner input = new Scanner(System.in);
10        try{
11            System.out.println("请输⼊第⼀个数:");
12            int one = Int();
13            System.out.println("请输⼊第⼆个数:");
14            int two = Int();
15            System.out.println("两数相除结果为:"+one/two);
16        }catch(InputMismatchException e){
17            System.out.println("请输⼊整数");
18        }catch(ArithmeticException e){
19            System.out.println("除数不能为零");
20        }catch(Exception e){
21            System.out.println("程序执⾏中出现异常");
22        }finally{
23            System.out.println("程序执⾏结束!");
24        }
25
26
27
28    }
29
30 }
⼆、使⽤throws关键字声明将要抛出何种类型的异常
语法
public void ⽅法吗(参数)throws 异常列表{ throw new Exception(); }
⽰例如下:
1 public class ThrowDemo {
2
3    public static void main(String[] args) {
4
5        ThrowDemo td = new ThrowDemo();
6        try {
7            td.test(10, 0);
8        } catch (Exception e) {
9            System.out.println("异常抛出");
10        }
11    }
12
13    public void test(int a,int b) throws Exception{
14        int c = a/b;
15        System.out.println("计算结果为:"+c);
16
17    }
18
19 }
三、⾃定义异常类nullpointerexception为什么异常
有的时候我们抛出的异常在Throwable类中没有定义,就需要我们⾃⼰⾃定义⼀个异常的类,⽐如我们实际开发中需要⽤到⼀个“开车别喝酒”的异常,我们就可以定义⼀个这样的异常类来处理我们项⽬中需要处理的异常。
⾃定义异常类的语法:
class ⾃定义异常类 extends 异常类型{}
⾃定义异常类需要继承和它类型相近的Throwable类⾥⾯的⼦类,或者是我们直接让⾃定义异常类继承E
xception类,⽰例如下:
1 /**
2  * ⾃定义⼀个异常类
3  * @author lenovo
4  *
5  */
6 public class MyThrow extends Exception{
7
8    public MyThrow(){
9
10    }
11
12    public MyThrow(String mess){
13        super(mess);
14    }
15 }
使⽤这个异常类的⽰例如下:
1 public class ChainTest {
2
3    /**
4      * test1():抛出"喝⼤了"异常;
5      * test2():调⽤test1(),捕获"喝⼤了"异常,并且包装成运⾏时异常,继续抛出;
6      * main⽅法中调⽤test2(),尝试捕获test2()⽅法抛出的异常
7      */
8
9    public static void main(String[] args) {
10        ChainTest ct = new ChainTest();
11        ct.test2();
12    }
13
14    public void test1() throws MyThrow{
15        throw new MyThrow("喝酒别开车!");
16    }
17
18    public void test2(){
19        try {
20            test1();
21        } catch (MyThrow e) {
22            RuntimeException newExc = new RuntimeException("司机⼀滴酒,亲⼈两⾏泪~~");
23            newExc.initCause(e);
24            throw newExc;
25        }
26    }
27
28 }
运⾏结果:
Exception in thread "main" java.lang.RuntimeException: 司机⼀滴酒,亲⼈两⾏泪~~
at st2(ChainTest.java:24)
at xbw.ChainTest.main(ChainTest.java:13)
Caused by: xbw.MyThrow: 喝酒别开车!
at st1(ChainTest.java:17)
at st2(ChainTest.java:22)
... 1 more
Java异常处理实际应⽤中的经验与总结:
1、处理运⾏时异常时,采⽤逻辑去合理规避同时辅助try-catch处理;
2、在多重catch块后⾯,可以加⼀个catch(Exception)来处理可能会被遗漏的异常;
3、对于不确定的代码,也可以加上try-catch,处理潜在异常;
4、尽量去处理异常,切忌只是简单的调⽤printStackTrace()去打印输出;
5、具体如何处理异常,要根据不同的业务需求和异常类型去决定;
6、尽量添加finally语句块去释放占⽤的资源。