zoukankan      html  css  js  c++  java
  • Java的error的捕获

      通常来说,大家都是对Java中的Exception进行捕获和进行相应的处理,有些人说,error就无法捕获了。其实,error也是可以捕获的。Error和Exception都是Throwable的子类。既然可以catch Throwable,那么error也是可以catch的。

    下面给个小例子,来验证一下error的捕获。

    public class TestCatchError extends Error{
        
        private static final long serialVersionUID = -351488225420878020L;
    
        public TestCatchError(){
            super();
        }
        
        public TestCatchError(String msg){
            super(msg);
        }
        
        public static void main(String[] args) {
            try {
                throw new TestCatchError("test catch error");
            } catch (Throwable t) {
                System.out.println("step in the catch ~");
                t.printStackTrace();
            }
        }
    }

    运行结果:

    step in the catch ~
    TestCatchError: test catch error
    at TestCatchError.main(TestCatchError.java:23)

      如此~

  • 相关阅读:
    编程路上有你们陪着值了
    我是屌丝程序猿,我为自己代言.
    JSON转换类
    深入理解requestAnimationFrame
    CentOS部署yapi
    前端三种路由方式
    yarn安装使用
    三大框架对比
    es6异步编程
    JS原型链
  • 原文地址:https://www.cnblogs.com/Scott007/p/3093617.html
Copyright © 2011-2022 走看看