zoukankan      html  css  js  c++  java
  • exception 打印出异常栈踪迹

    Java异常抛出使用e.printStackTrace(),打印出抛出的异常栈踪迹,

    如果你在catch中继续抛出这个异常,那么e.printStackTrace()也能跟踪到抛出异常的地方,

    使用throw new RunTimeException(e.fillInStackTrace()),改变异常栈踪迹。

    public class TestException {
        public static void main(String[] args) {
            TestException testException = new TestException();
            testException.testException();
        }
    
        public void testException()
        {
            try {
                int num  = 1/0;
    
            }catch (RuntimeException e)
            {    
                throw new RuntimeException(e.fillInStackTrace());
           //
    throw new RuntimeException(e);
          }
      }
    }

    console打印

    Exception in thread "main" java.lang.RuntimeException: java.lang.ArithmeticException: / by zero
        at com.jxufe.study.jsonstudy.test.TestException.testException(TestException.java:22)
        at com.jxufe.study.jsonstudy.test.TestException.main(TestException.java:11)
    Caused by: java.lang.ArithmeticException: / by zero
        ... 2 more

    将上面的

    throw new RuntimeException(e.fillInStackTrace()); 改为   throw new RuntimeException(e);

    console打印

    Exception in thread "main" Disconnected from the target VM, address: '127.0.0.1:57316', transport: 'socket'
    java.lang.RuntimeException: java.lang.ArithmeticException: / by zero
        at com.jxufe.study.jsonstudy.test.TestException.testException(TestException.java:22)
        at com.jxufe.study.jsonstudy.test.TestException.main(TestException.java:11)
    Caused by: java.lang.ArithmeticException: / by zero
        at com.jxufe.study.jsonstudy.test.TestException.testException(TestException.java:17)
        ... 1 more

    跟踪到抛出异常的第17行代码 int num = 1/0;

  • 相关阅读:
    使用slot-scope复制vue中slot内容
    pre-commit钩子,代码质量检查
    vue 顶级组件
    Vue源码探究-事件系统
    vue 配置多页面应用
    vuex commit保存数据技巧
    在VUE中,关于CKEditor使用
    使用el-checkbox实现全选,点击失效没有反应
    vue组件之间的通信
    不做Next,争做Nest——庆科首届智能硬件创新设计大赛产生决赛12强
  • 原文地址:https://www.cnblogs.com/alway-july/p/6557094.html
Copyright © 2011-2022 走看看