Java中的异常

一、总结

1.Java中的异常分为必须处理的异常和可以不处理的异常
(1)必须处理的异常
① 自己处理:
  try {
    可能导致异常出现的代码
  } catch (异常类型 e) {
    捕捉到异常后的处理代码
  } finally {
    无论是否出现异常都会执行的代码 //注意一定会执行,可能导致逻辑上的错乱。
  }

② 让别人处理:throws
  public static int div(int m, int n) throws(异常类型1) {
    可能出异常类型1的代码
  }

③ 有些异常需要自己处理一部分后使用throw再次抛出这个异常,外部也做相应的处理。


(2)可以不处理的异常,比如runtime异常,至少可以编译过。而可见查异常编译都无法过,必须要在代码中try catch处理或者throws这个异常。


2.可以先捕获具体的可能出现的运行时异常,但是由于运行时异常有很多,可能不可能都列出来捕获完此时可以最后捕获父类运行时异常,这样所有的运行时异常都可以捕获到了。运行时异常作为父类应该放在最后一个catch()里面,否则会覆盖前面的子类的异常。

3.若 try catch 语句块里面有throw或return语句的话会先执行finally语句块,然后再返回来执行throw或return语句。(若finally语句块中也有return语句,那么就直接退出了,不会再返回去执行throw或return语句了,因此尽量避免在finally中return) 

4.优秀博文:http://blog.csdn.net/hguisu/article/details/6155636 <未看>

5.如果不处理异常,一旦异常发生时程序会退出。

6.在JDK API的chm手册中的Integer类中有将命令行参数转换为int的方法。先检索Integer然后在它里面找parseInt()方法。使用JDK提供的方法不需要import包。

7.try catch的异常就是串口打印的异常类型,也就是串口报什么异常就在catch()中写成什么异常。

8.可检查异常和不可查异常(运行时异常)

9.若函数内没有处理异常,那么异常将会一层一层地往上抛,无论函数签名后是否有throws exceptionType.
public static int div(int m, int n) throws ArithmeticException {  //若函数内部没有处理这个异常,这里写不写throws都会抛出给上层函数
  int r = m / n;
  return r;
}

10.函数内部(catch)处理异常后,函数外部就不会再收到异常了。除非catch语句块中再次throw出异常。即便是函数头上加throws ArithmeticException也不会抛出此异常,因为此异常在函数内部已经处理了。

11.其实函数内部throw的异常函数内部也是可以捕捉的,此时此函数就不会因调用throw而返回退出。
  try {
    throw new Exception("My Error");
  } catch (Exception e2) {
    System.out.println("div :"+ e2);
  }

12.方法内部throw Exception必须自己catch()或者声明被抛出方法外

error: unreported exception Exception; must be caught or declared to be thrown
        throw new Exception("Invalid Arguement");
        ^
public void addNode(Node node) {
    throw new Exception("Invalid Arguement");
}
//修正:
public void addNode(Node node) throws Exception {
    throw new Exception("Invalid Arguement");        //注意这里是throw,上面是throws!
}

之后若每一层父级调用只要没有使用try...catch来捕获异常,都要在函数声明处加throws Exception!

二、试验Demo

/* java Div 6 2
 * 6/2=3
 */

public class Div9 {

    public static void main(String args[]) {
        int m = 0;
        int n = 0;
        int r = 0;
        
        System.out.println("Begin of div");
        try {
            m = Integer.parseInt(args[0]);
            n = Integer.parseInt(args[1]);
            r = div(m, n);
        } catch (ArithmeticException e) {
            System.out.println("main :"+e);
        } catch (NumberFormatException e) {
            System.out.println("main :"+e);
        } catch (RuntimeException e) {
            System.out.println("main :"+e);
        }
        System.out.println("End of div");

        System.out.println(m+"/"+n+"="+r);
        
    }

//    public static int div(int m, int n) throws ArithmeticException {
    public static int div(int m, int n) {    // also the same    
        int r = 0;

        try {
            r = m / n;
        } catch (ArithmeticException e) {
            System.out.println("div :"+e);
            throw e;
        } finally {
            System.out.println("finally of div");
            //return r;
        }
        return r;
    }
}
原文地址:https://www.cnblogs.com/hellokitty2/p/10425545.html