zoukankan      html  css  js  c++  java
  • java基础篇 之 异常丢失

    我们看如下代码:

    @Slf4j
    public class Test {
        public static void main(String[] args) {
            try {
                try {
                    test();
                } finally {
                    test2();
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
    
        }
    
        public static void test() {
            System.out.println("test方法执行了");
            throw new RuntimeException("veryImportant exception");
        }
    
        public static void test2() {
            System.out.println("test2");
            throw new CustomeException("common exception");
        }
    }
    

    执行结果如下:

    
    

    我们可以看到,在执行的适合,一个veryImportant exception丢失了,而抛出了一个common exception,这是相当严重的缺陷,因为异常可能会以一种比前面例子所示更微妙和难以察觉的方式完全丢失。相比之下,C++把“前一个异常还没处理就抛出下一个异常”的情形看成是糟糕的错误。

    一种更加糟糕的编程手法如下所示:

    package com.study.spring.transaction.controller;
    
    import com.study.spring.transaction.CustomeException;
    import lombok.extern.slf4j.Slf4j;
    
    /**
     * @Author: dmz
     * @Description:
     * @Date: Create in 2:40 2019/4/21
     */
    @Slf4j
    public class Test {
        public static void main(String[] args) {
            try {
                try {
                    test();
                } finally {
                    return;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
    
        }
    
        public static void test() {
            System.out.println("test方法执行了");
            throw new RuntimeException("veryImportant exception");
        }
    
        public static void test2() {
            System.out.println("test2");
            throw new CustomeException("common exception");
        }
    }
    

    可以看到,在finally快中,我们直接将函数返回了,这个适合执行代码,会发现,即使抛出了异常,也不会有任何输出。

    这种情况下我们怎么办呢?我觉得主要就是多加日志,虽然程序不会主动输出什么,但是我们可以在出现错误的地方自己打印日志

    @Slf4j
    public class Test {
        public static void main(String[] args) {
            try {
                try {
                    test2();
                } finally {
                    return;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
    
        }
    
        public static void test() {
            System.out.println("test方法执行了");
            throw new RuntimeException("veryImportant exception");
        }
    
        public static void test2() {
            System.out.println("test2");
            CustomeException common_exception = new CustomeException("common exception");
            log.error("test2调用失败", common_exception);
            throw common_exception;
        }
    

    调用结果:

    test2
    00:46:09.508 [main] ERROR com.study.spring.transaction.controller.Test - test2调用失败
    com.study.spring.transaction.CustomeException: common exception
    	at com.study.spring.transaction.controller.Test.test2(Test.java:33)
    	at com.study.spring.transaction.controller.Test.main(Test.java:16)
    
    Process finished with exit code 0
    
    
  • 相关阅读:
    Web3与智能合约交互实战
    详解 Solidity 事件Event
    iOS App迁移(App Transfer)注意点
    IDFA踩坑记录
    iOS error: -34018
    Apple 的命令行交付工具“Transporter”
    关于iOS UIWebView 加载网页,点击网页内某些控件导致 Application 'UIKitApplication:xxx.xxx.xxx' was killed by jetsam.
    苹果应用内支付详解以及如何预防刷单等行为
    iOS “弱账号” 暗转 “强账号”
    好用的敏捷开发软件推荐
  • 原文地址:https://www.cnblogs.com/daimzh/p/12854434.html
Copyright © 2011-2022 走看看