Java异常信息处理
生活随笔
收集整理的這篇文章主要介紹了
Java异常信息处理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;import org.junit.Test;/*** * Description: 異常信息打印(controller繼承此類即可調(diào)用)* * @author: Byron Wang* @version: V1.0*/
public class CommonController {/*** Description:獲取子類調(diào)用處方法名字* @return 調(diào)用處方法名*/public String getCurMethodName() {// 獲取調(diào)用處方法的名稱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: ", "異 常 提 示 信 息:::", "異常發(fā)生: [", "異常捕獲: [", "\t異常信息: [", "日期時(shí)間: " };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("解析參數(shù)出錯(cuò)");}public static void getException2() throws IOException {getException();}@Testpublic void testException() {try {getException2();} catch (IOException e) {e.printStackTrace();System.out.println(getExceptionLogMsg(e, CHINESE));}}@Testpublic 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));// }
}}
?
轉(zhuǎn)載于:https://www.cnblogs.com/techroad4ca/p/5009590.html
總結(jié)
以上是生活随笔為你收集整理的Java异常信息处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。