捕获异常信息的详细信息,有助于我们排查问题。这里给出一个通用的异常信息获取工具类,main函数给出了demo,可以看到效果
/**
 * 项目名称(中文)
 * 项目名称(英文)
 * Copyright (c) 2018 ChinaPay Ltd. All Rights Reserved.
 */
package com.figo.study.utils;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
 * 获取抛出的异常详细信息 .
 * 
 * @author figo
 * @version 1.0 2018-10-12 改订
 * @since 1.0
 */
public class ExceptionDetailUtil {
    /**
     * 除0测试.
     * 
     * @param args
     */
    public static void main(String[] args) {
        try {
            int a = 10, b = 0;
            int c = a / b;
            System.out.println(c);
        } catch (Exception e) {
            System.out.println(e.getStackTrace());// 这么写是不能打印出详细错误信息的
            System.out.println(e.getMessage());// 这么写只能知道出了什么错,也不能打印出详细错误信息的
            System.out.println(getExceptionDetail(e));//这个可以
            System.out.println(getThrowableDetail(e));//这个可以
        } catch (Error e) {
            System.out.println(getExceptionDetail(e));
        }
    }
    /**
     * 获取异常详细信息,知道出了什么错,错在哪个类的第几行 .
     * 
     * @param ex
     * @return
     */
    public static String getExceptionDetail(Exception ex) {
        String ret = null;
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            PrintStream pout = new PrintStream(out);
            ex.printStackTrace(pout);
            ret = new String(out.toByteArray());
            pout.close();
            out.close();
        } catch (Exception e) {
        }
        return ret;
    }
    /**
     * 获取异常详细信息,知道出了什么错,错在哪个类的第几行 .
     * 
     * @param ex
     * @return
     */
    public static String getExceptionDetail(Error ex) {
        String ret = null;
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            PrintStream pout = new PrintStream(out);
            ex.printStackTrace(pout);
            ret = new String(out.toByteArray());
            pout.close();
            out.close();
        } catch (Exception e) {
        }
        return ret;
    }
    /**
     * 获取异常详细信息,知道出了什么错,错在哪个类的第几行 .
     * 
     * @param e
     * @return
     */
    public static String getThrowableDetail(Throwable ex) {
        StringWriter sw = new StringWriter();
        try {
            PrintWriter pw = new PrintWriter(sw, true);
            ex.printStackTrace(pw);
            pw.flush();
            sw.flush();
        } catch (Exception e) {
        }
        return sw.toString();
    }
}
运行效果

 
                   
                   
                   
                   
                             本文介绍了一种用于捕获和解析异常信息的工具类,该工具能够详细展示异常发生的具体位置,包括出错的类名和行号,有助于快速定位和解决问题。
本文介绍了一种用于捕获和解析异常信息的工具类,该工具能够详细展示异常发生的具体位置,包括出错的类名和行号,有助于快速定位和解决问题。
           
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                  
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            