zoukankan      html  css  js  c++  java
  • 关于打印日志几个方法的列举

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.StringWriter;

    import org.junit.Test;

    public class ExceTest {
        @Test
        public static void main(String[] args){
            try {
    //            throw new Exception("发生异常");
                Integer.valueOf("a");
            } catch (Exception e) {
                //只会获得具体的异常名称. 比如说NullPoint 空指针,就告诉你说是空指针...
                System.out.println("e.getMessage 异常信息:"+e.getMessage());
                
                //来提供一个针对地区方言的错误信息
                System.out.println("e.getLocalizedMessage 异常信息:"+e.getLocalizedMessage());
                
                //e.getCause()有可能返回null
                System.out.println("e.getCause 异常信息:"+e.getCause().getMessage());
                
                StringWriter sw = new StringWriter();  
                PrintWriter pw = new PrintWriter(sw);  
                e.printStackTrace(pw);
                System.out.println(sw.toString());
                
                try {
                    ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
                    //会打出详细异常,异常名称,出错位置,便于调试用,printStackTrace 有三个重载的方法
                    e.printStackTrace(new java.io.PrintWriter(buf, true));
                    String expMessage = buf.toString();
                    System.out.println("e.printStackTrace 异常信息: "+expMessage);
                    buf.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

  • 相关阅读:
    python之路_django入门项目(老师表)
    python之路_django入门项目(学生表)
    python之路_初识django框架
    python之路_前端基础之Bootstrap JS
    python之路_前端基础之Bootstrap 组件
    python之路_前端基础之Bootstrap CSS
    python之路_登录验证及表格增删改作业
    Spring Boot CLI安装
    Spring Boot 所提供的配置优先级顺序
    Intellij IDEA 14.x 中的Facets和Artifacts的区别
  • 原文地址:https://www.cnblogs.com/robertsun/p/5129904.html
Copyright © 2011-2022 走看看