Java异常信息处理

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;

/**
 * 
 * Description: 异常信息打印(controller继承此类即可调用)
 * 
 * @author: Byron Wang
 * @version: V1.0
 */
public class CommonController {

    /**
     * Description:获取子类调用处方法名字
     * @return 调用处方法名
     */
    public String getCurMethodName() {
        // 获取调用处方法的名称
        return Thread.currentThread().getStackTrace()[2].getMethodName();
    }

    public static final int ENGLISH = 0;
    public static final int CHINESE = 1;

    private static final int MSG_NUM = 5;
    private static final int NOTICE_INDEX = 0;
    private static final int HAPPEN_INDEX = 1;
    private static final int CATCH_INDEX = 2;
    private static final int MSGE_INDEX = 3;
    private static final int DATETIME_INDEX = 4;

    /**
     * Description: 获取异常日志信息(本方法不适用于单元测试中)
     * 
     * @param exception
     * @param lang
     * @return
     */
    public static String getExceptionLogMsg(Exception exception, int lang) {
        String[] header = { "DEBUG MESSAGE:::", "exception happen: [", 
                "exception catch : [", "\tmessage: [", "datetime: ", 
                "异 常 提 示 信 息:::", "异常发生: [", 
                "异常捕获: [", "\t异常信息: [", "日期时间: " };
        String lineTail = "]\n\t\t";
        int baseindex = lang * MSG_NUM;
        StackTraceElement[] stes = exception.getStackTrace();
        StackTraceElement happenTrace = stes[0];
        StackTraceElement catchTrace = stes[stes.length - 1];
        // 获取异常信息,若为空则返回异常名称
        String message = exception.getMessage();
        if (message == null || message.trim().length() == 0 
                || "null".equalsIgnoreCase(message)) {
            message = exception.getClass().getName();
        }

        StringBuilder builder = new StringBuilder(header[baseindex + NOTICE_INDEX]);
        String nowStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        builder.append(header[baseindex + DATETIME_INDEX]).append(nowStr);
        builder.append(header[baseindex + MSGE_INDEX])
                .append(message).append(lineTail);
        builder.append(header[baseindex + HAPPEN_INDEX])
                .append(joinExceptionPosition(happenTrace));
        builder.append(header[baseindex + CATCH_INDEX])
                .append(joinExceptionPosition(catchTrace));
        return builder.toString();
    }

    /**
     * Description: 拼接异常位置
     * 
     * @param element
     * @return
     * 
     */
    private static String joinExceptionPosition(StackTraceElement element) {
        String lineTail = "]\n\t\t";
        String seprator = "..";
        StringBuilder builder = new StringBuilder();
        builder.append(element.getClassName()).append(seprator)
                .append(element.getMethodName()).append(seprator)
                .append(element.getLineNumber()).append(lineTail);
        return builder.toString();
    }

    /**
     * TEST
     * 
     * @throws IOException
     */
    public static void getException() throws IOException {
        throw new IOException("解析参数出错");
    }

    public static void getException2() throws IOException {
        getException();
    }

    @Test
    public void testException() {
        try {
            getException2();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(getExceptionLogMsg(e, CHINESE));
        }
    }

    @Test
    public void testExceptionNoMsg() {
        try {
            throw new StringIndexOutOfBoundsException();
        } catch (StringIndexOutOfBoundsException e) {
            e.printStackTrace();
            System.out.println(getExceptionLogMsg(e, CHINESE));
        }
    }

    public static void main(String[] args) {
        try {
            throw new StringIndexOutOfBoundsException();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(getExceptionLogMsg(e, CHINESE));
        }
        // try {
        // getException2();
        // } catch (IOException e) {
        // e.printStackTrace();
        // System.out.println(getExceptionLogMsg(e, CHINESE));
        // }
    }

}

 

转载于:https://www.cnblogs.com/techroad4ca/p/5009590.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值