异常(4)----对编译异常的处理

版权声明:未经同意,严禁转载 https://blog.csdn.net/pengchengliu/article/details/82767509

一、对编译异常的处理                                                                                                                                点击此处返回总目录

二、两种处理方式的比较

一、对编译异常的处理

1. 对于编译异常,要么使用try...catch处理掉,要么继续抛出。【例1】【例2】【例3】

2. 在API文档中,带有throws的方法,抛出的异常都是编译异常。所以当调用带有throws的方法(自己写方法的时候也尽量满足:编译异常写throws,运行异常不写throws)时,必须对异常进行捕获处理或者抛出。

3. 如果不对带有throws的方法(抛出编译异常的方法)进行异常处理或抛出,则会报错。【例4】【例5】

例1:当调用带有throws关键字的方法时,使用try...catch对异常进行捕获处理。

package cn.itcast.demo03;

public class Test {
    public static void main(String[] args) {
        int[] arr = null;
        int i;
        try{
            i = fun(arr);
            System.out.println(i);
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("aaaaa");
        }
        System.out.println("bbbbb");
    }
    

    //自定义的带有throws的方法,抛出的时非RuntimeException的异常。
    public static int fun(int[] arr) throws Exception{
        if(arr == null){
            throw new Exception("数组不存在");
        }
        return arr[3];
    }
}

 

运行结果:

java.lang.Exception: 数组不存在
    at cn.itcast.demo03.Test.fun(Test.java:19)
    at cn.itcast.demo03.Test.main(Test.java:8)
aaaaa
bbbbb

例2:当调用带有throws关键字的方法时,程序处理不了,则继续抛出。

package cn.itcast.demo03;

public class Test {
    public static void main(String[] args) throws Exception { //因为main方法调用了带有throws关键字的方法fun(),按习惯抛出的就是                                                                                           非RuntimeException的异常,所以必须对fun()可能抛出的异常进行处理。                                                                                           而main也不知道怎么处理,所以继续抛出。
        int[] arr = null;
        fun(arr);
        System.out.println("bbbbb");
    }
    
    public static void fun(int[] arr) throws Exception{
        throw new Exception("数组不存在");
    }
}

运行结果:

Exception in thread "main" java.lang.Exception: 数组不存在
    at cn.itcast.demo03.Test.fun(Test.java:11)
    at cn.itcast.demo03.Test.main(Test.java:6)
 

例3:parse()方法带有throws关键字,应该捕获处理或抛出。

package cn.itcast.demo05;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
    public static void main(String[] args) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date  = sdf.parse("2088-8-8");
        System.out.println(date);
    }
}

例4:如果不对带有throws关键字的方法进行异常处理或抛出,则会报错。

package cn.itcast.demo04;

public class Test {
    public static void main(String[] args) {
        int[] arr = null;
        int i = fun(arr);
        System.out.println(i);            
    }
    
    public static int fun(int[] arr) throws Exception{
        if(arr == null){
            throw new Exception("没有该数组");
        }
        return arr[2];
    }
    
}

运行结果:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type Exception

    at cn.itcast.demo04.Test.main(Test.java:6)

例5:如果不对带有throws关键字的方法进行异常处理或抛出,则会报错。

package cn.itcast.demo05;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
    public static void main(String[] args){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date  = sdf.parse("2088-8-8");
        System.out.println(date);
    }
}

运行结果:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type ParseException

    at cn.itcast.demo05.Test.main(Test.java:9)
 

二、两种处理方式的比较

当调用一个带有throws的方法时,是选择try...catch呢,还是选择throws呢?

按照开发的要求,只要用了带有throws的方法,就应该try...catch。老写throws的结果就是导致程序停了。所以不建议throws。

猜你喜欢

转载自blog.csdn.net/pengchengliu/article/details/82767509