开发者社区> 问答> 正文

异常处理方法

异常处理方法

展开
收起
珍宝珠 2020-02-13 17:23:29 1503 0
1 条回答
写回答
取消 提交回答
  • 使用 System 类的 System.err.println() 来展示异常的处理方法:

    class ExceptionDemo
    {
        public static void main(String[] args) {
            try {
                throw new Exception("My Exception");
            } catch (Exception e) {
                System.err.println("Caught Exception");
                System.err.println("getMessage():" + e.getMessage());
                System.err.println("getLocalizedMessage():" + e.getLocalizedMessage());
                System.err.println("toString():" + e);
                System.err.println("printStackTrace():");
                e.printStackTrace();
            }
        }
    }
    
    

    以上代码运行输出结果为:

    Caught Exception
    getMessage():My Exception
    getLocalizedMessage():My Exception
    toString():java.lang.Exception: My Exception
    printStackTrace():
    java.lang.Exception: My Exception
       at ExceptionDemo.main(ExceptionDemo.java:5)
    
    2020-02-13 17:23:58
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载