Java异常处理

source:http://blog.java1234.com/index.html?typeId=1

异常:程序在执行过程中,出现意外;类似开车路上出了交通事故;

在java中,我们用try-catch来捕获异常 

try...cacth...finally

//Exception是异常类的老祖宗,在捕获异常的时候,假如不确定会抛出什么异常,可以写多个异常捕获:注意 由上往下的异常 必须范围同级别或者更高;否则编译报错;
public static void main(String[] args) {
        String str="123a";
        try{
            int a=Integer.parseInt(str);          
        }catch(NullPointerException e){
            e.printStackTrace();
        }catch(NumberFormatException e){
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("继续执行");
}

try...cacth...finally 假如有某种需求,不管有没有发生异常,比如执行某些代码,这时候,finally就派上用场了;

public class Demo {
 
    public static void testFinally(){
        String str="123a";
        try{
            int a=Integer.parseInt(str);
            System.out.println(a);
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("exception");
            return;
        }finally{
            System.out.println("finally end");
        }
        System.out.println("end");
    }
     
    public static void main(String[] args) {
        testFinally();
    }
}

运行输出:

java.lang.NumberFormatException: For input string: "123a"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at com.java1234.chap04.sec02.Demo2.testFinally(Demo2.java:8)
    at com.java1234.chap04.sec02.Demo2.main(Demo2.java:21)
exception
finally end

throws和throw关键字

throws表示当前方法不处理异常,而是交给方法的调用出去处理;

throw表示直接抛出一个异常;

public class Demo {
 
    /**
     * 把异常向外面抛
     * @throws NumberFormatException
     */
    public static void testThrows()throws NumberFormatException{
        String str="123a";
        int a=Integer.parseInt(str);
        System.out.println(a);
    }
     
    public static void main(String[] args) {
        try{
            testThrows();  
            System.out.println("here");
        }catch(Exception e){
            System.out.println("我们在这里处理异常");
            e.printStackTrace();
        }
        System.out.println("I'm here");
    }
}

运行输出:

我们在这里处理异常
java.lang.NumberFormatException: For input string: "123a"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at com.java1234.chap04.sec03.Demo1.testThrows(Demo1.java:11)
    at com.java1234.chap04.sec03.Demo1.main(Demo1.java:17)
I'm here
public class Demo {
 
    public static void testThrow(int a) throws Exception{
        if(a==1){
            // 直接抛出一个异常类
            throw new Exception("有异常");
        }
        System.out.println(a);
    }
     
    public static void main(String[] args) {
        try {
            testThrow(1);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

运行输出:

1 java.lang.Exception: 有异常
2     at com.java1234.chap04.sec03.Demo2.testThrow(Demo2.java:8)
3     at com.java1234.chap04.sec03.Demo2.main(Demo2.java:15)

Exception 和 RuntimeException 区别

Exception是检查型异常,在程序中必须使用try...catch进行处理;

RuntimeException是非检查型异常,例如NumberFormatException,可以不使用try...catch进行处理,但是如果产生异常,则异常将由JVM进行处理;

RuntimeException最好也用try...catch捕获;

 1 public class Demo1{
 2  
 3     /**
 4      * 运行时异常,编译时不检查,可以不使用try...catch捕获
 5      * @throws RuntimeException
 6      */
 7     public static void testRuntimeException()throws RuntimeException{
 8         throw new RuntimeException("运行时异常");
 9     }
10      
11     /**
12      * Exception异常,编译时会检查,必须使用try...catch捕获
13      * @throws Exception
14      */
15     public static void testException()throws Exception{
16         throw new Exception("Exception异常");
17     }
18      
19     public static void main(String[] args) {
20         try {
21             testException();
22         } catch (Exception e) {
23             // TODO Auto-generated catch block
24             e.printStackTrace();
25         }
26          
27         testRuntimeException();
28     }
29 }

运行输出:

java.lang.Exception: Exception异常

at com.java1234.chap04.sec04.Demo1.testException(Demo1.java:18)

at com.java1234.chap04.sec04.Demo1.main(Demo1.java:23)

Exception in thread "main" java.lang.RuntimeException: 运行时异常

at com.java1234.chap04.sec04.Demo1.testRuntimeException(Demo1.java:10)

at com.java1234.chap04.sec04.Demo1.main(Demo1.java:29)

自定义异常

自定义异常要继承自Exception;

 1 /**
 2  * 自定义异常,继承自Exception
 3  * @author user
 4  *
 5  */
 6 public class CustomException extends Exception{
 7  
 8     public CustomException(String message) {
 9         super(message);
10     }
11  
12 }
13 public class TestCustomException {
14  
15     public static void test()throws CustomException{
16         throw new CustomException("自定义异常");
17     }
18      
19     public static void main(String[] args) {
20         try {
21             test();
22         } catch (CustomException e) {
23             // TODO Auto-generated catch block
24             e.printStackTrace();
25         }
26     }
27 }

运行输出:

com.java1234.chap04.sec05.CustomException: 自定义异常

at com.java1234.chap04.sec05.TestCustomException.test(TestCustomException.java:6)

at com.java1234.chap04.sec05.TestCustomException.main(TestCustomException.java:11)

原文地址:https://www.cnblogs.com/CareyZhao/p/10329774.html