Java异常处理

Java的异常处理有两种方式:
- 默默解决。使用try...catch
- 甩锅。使用throwthrows

通常,我们喜欢用try/catch捕获异常,然后对异常进行处理。但是有时候,我们也会不予理会,直接将异常扔出去,让调用者自己去处理异常。当然对调用者来说,它也有两种同样的选择:自己处理掉,或者再往外扔,交给自己的调用者,由新的调用者继续背锅。

以下是几种正常与不正常的异常处理方式示例,从中可以看出异常处理的流程:

测试类一

正常抛出异常。声明throwsthrow

package Exception;

public class ExceptionThrow {
    public static void foo() throws Exception {
        throw new Exception("throw my exception.");
    }
}

测试类二

声明throws,但是不throw(不抛出异常):

package Exception;

public class ExceptionNotThrow {
    public static void bar() throws Exception {
        ;
    }
}

测试类三

异常已经由本方法已处理,但是仍声明throws

package Exception;

public class ExceptionHandledButThrow {
    public static void foobar() throws Exception {
        try {
            ExceptionThrow.foo();
        } catch (Exception e) {
            System.out.println("Catch an exception: " + e.getMessage());
        }
    }
}

测试类四、五:

package Exception;

import java.util.concurrent.TimeoutException;

public class InnerException {
    public static void foo() throws TimeoutException {
        throw new TimeoutException("inner timeout exception");
    }
}

package Exception;

public class OuterException {
    public static void bar() throws Exception {
        try {
            InnerException.foo();
        } catch (NullPointerException e) {
            e.getMessage();
            System.out.println("Only catch NullPointerException.");
        }
    }
}

测试类:

package Exception;

public class Main {

    public static void main(String[] args) {

        System.out.println("=========Case 1=========");

        try {
            ExceptionThrow.foo();
            System.out.println("=>1: Should never be printed!!!");
        } catch (Exception e) {
            if (e.getMessage().contains("my")) {
                System.out.println("Exception happens: " + e.getMessage());
//                e.printStackTrace();
            } else {
                System.out.println("=>2: Should never be printed!!!");
            }
        } finally {
            System.out.println("=>3: Should always be printed!!!");
        }

        System.out.println("=========Case 2=========");

        try {
            ExceptionNotThrow.bar();
            System.out.println("=>1: Should be printed!!!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("=>2: Should never be printed!!!");
        } finally {
            System.out.println("=>3: Should always be printed!!!");
        }

        System.out.println("=========Case 3=========");

        try {
            ExceptionHandledButThrow.foobar();
            System.out.println("=>1: Should be printed!!!");
        } catch (Exception e) {
            System.out.println("=>2: Should never be printed!!!");
        }

        System.out.println("=========Case 4=========");

        try {
            OuterException.bar();
        } catch (Exception e) {
            System.out.println("In main: " + e.getMessage());
    }
}

结果

=========Case 1=========
Exception happens: throw my exception.
=>3: Should always be printed!!!
=========Case 2=========
=>1: Should be printed!!!
=>3: Should always be printed!!!
=========Case 3=========
Catch an exception: throw my exception.
=>1: Should be printed!!!
=========Case 4=========
In main: inner timeout exception

分析

  • Case1:try在捕获到异常之后,直接进入catch,运行catch之内的部分。catch可以根据异常的情况,进行不同的处理逻辑。
  • Case2:如果try捕获不到异常,将继续执行try后面的部分,catch部分则直接跳过。(eg:ExceptionNotThrow.bar(),只声明throws却不throw,相当于方法永不会抛出异常,则处理这个方法异常的try永远都不会捕获到异常)
  • Case3:如果本方法(eg:ExceptionHandledButThrow.foobar())已经处理了异常,还声明throws,则调用者同样在try块里永远都不会捕获到异常(因为异常已经被处理了),但是调用者必须有try/catch,因为所调用的方法(foobar())虽然不抛异常,但是坑爹地声明了throws
  • Case4:如果本方法没有catch住被调用者抛出的异常,则必须将异常继续throws出去,否则会报错。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值