zoukankan      html  css  js  c++  java
  • 【异常-举例3:打印异常信息】

    package com.test;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    /**
     * @author shusheng
     * @description
     * @Email shusheng@yiji.com
     * @date 2018/10/10 13:49
     */
    public class ExceptionDemo3 {
        /**
         *在 try 里面发现问题后,jvm 会帮我们生成一个异常对象,然后把这个对象抛出,
         * 和 catch里面的类进行匹配。
         *如果该对象是某个类型的,就会执行该 catch 里面的处理信息。
         *
         *异常中要了解的几个方法:
         *public String getMessage():异常的消息字符串
         *调用此对象 getLocalizedMessage()方法的结果 (默认返回的是 getMessage()的内容)
         *public String toString():返回异常的简单信息描述
         *printStackTrace() 获取异常类名和异常信息,以及异常出现在程序中的位置。返回值 void。把信息输出在控制台。
        */
        public static void main(String[] args) {
            String s = "2014-11-20";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date d = sdf.parse(s); // 创建了一个 ParseException 对象,然后抛出去,和 catch里面进行匹配
                System.out.println(d);
            } catch (ParseException e) {
                e.printStackTrace();
                /**输出:
                 * java.text.ParseException: Unparseable date: "2014-11-20"
                 at java.text.DateFormat.parse(DateFormat.java:366)
                 at com.test.ExceptionDemo3.main(ExceptionDemo3.java:19)*/
                System.out.println("---------------------------------");
                System.out.println(e.getMessage());
                /**输出:Unparseable date: "2014-11-20"*/
                System.out.println("---------------------------------");
                 System.out.println(e.toString());
                /**java.text.ParseException: Unparseable date: "2014-11-20"*/
            }
            System.out.println("over");
        }
    
    }
    终身学习者
  • 相关阅读:
    迭代器
    逻辑量词
    How to distinguish between strings in heap or literals?
    Is the “*apply” family really not vectorized?
    power of the test
    The Most Simple Introduction to Hypothesis Testing
    析构函数允许重载吗?
    C++中析构函数的作用
    在C#中的构造函数和解析函数
    c++构造函数与析构函数
  • 原文地址:https://www.cnblogs.com/zuixinxian/p/9766101.html
Copyright © 2011-2022 走看看