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)); // } } }
Java异常信息处理的更多相关文章
- Java异常错误的面试题及答案
1) Java中什么是Exception? 这个问题经常在第一次问有关异常的时候或者是面试菜鸟的时候问.我从来没见过面高级或者资深工程师的 时候有人问这玩意,但是对于菜鸟,是很愿意问这个的.简单来说, ...
- java异常面试常见题目
在Java核心知识的面试中,你总能碰到关于 处理Exception和Error的面试题.Exception处理是Java应用开发中一个非常重要的方面,也是编写强健而稳定的Java程序的关键,这自然使它 ...
- 浅谈java异常[Exception]
学习Java的同学注意了!!! 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:589809992 我们一起学Java! 一. 异常的定义 在<java编程思想 ...
- 基础知识《十》java 异常捕捉 ( try catch finally ) 你真的掌握了吗?
本文转载自 java 异常捕捉 ( try catch finally ) 你真的掌握了吗? 前言:java 中的异常处理机制你真的理解了吗?掌握了吗?catch 体里遇到 return 是怎么处理 ...
- Java异常体系及分类
上图是基本的java异常体系结构. 主要分为2大类:Error和Exception 1.Error:描述了Java运行系统中的内部错误以及资源耗尽的情形.应用程序不应该抛出这种类型的对象,一般是由虚拟 ...
- Java异常之自定义异常
哎呀,妈呀,又出异常了!俗话说:"代码虐我千百遍,我待代码如初恋". 小Alan最近一直在忙着工作,已经很久没有写写东西来加深自己的理解了,今天来跟大家聊聊Java异常.Java异 ...
- 第11章 Java异常与异常处理
1.Java异常简介 1.什么是异常异常出现的时候代码会无法正常运行下去,会产生各种问题2.捕捉异常的作用提早发现异常,方便查找问题,并给出解决方法3.Java中的异常1.Java中所有不正常的类都是 ...
- java 异常
1.java异常 2.自定义抛出 3.运行时异常,程序有问题,让使用者可以改' ' 4.return 和 throw的区别 return 符合函数要求的值 throw 有问题的时候用它结束 ...
- 3.Java异常进阶
3.JAVA异常进阶 1.Run函数中抛出的异常 1.run函数不会抛出异常 2.run函数的异常会交给UncaughtExceptionhandler处理 3.默认的UncaughtExceptio ...
随机推荐
- 领域驱动设计(DDD)实现之路
2004年,当Eric Evans的那本<领域驱动设计——软件核心复杂性应对之道>(后文简称<领域驱动设计>)出版时,我还在念高中,接触到领域驱动设计(DDD)已经是8年后的事 ...
- Array,List,Struct可能被大家忽略的问题
Q1: 首先定义一个结构 public struct MyStruct { public int T; } 定义一个泛型List来存放结构体,然后访问第一个元素去修改T,输出T: List<My ...
- C语言字符串匹配函数
C语言字符串匹配函数,保存有需要时可以用: #include <stdio.h> #include <stdlib.h> #include <string.h> # ...
- LeetCode131:Palindrome Partitioning
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- fibonacci数列从a到b的个数
Description 我们定义斐波那契数列如下: f1=1 f2=2 f(n)=f(n-1)+f(n-2)(n>=3) 现在,给定两个数a和b,计算有多少个斐波那契数列中的数在a和b之间( ...
- [moka同学笔记]linux服务器防火墙的设置
网站突然打不开:服务器停止了,重启后,防火墙自动启动,导致网站打不开. 1.查看防火墙 systemctl status firewalld 2.关闭防火墙 systemctl stop firewa ...
- AI - Ideas
Idea: Comprehend code and re-factory code to more readable. The AI program can comprehend source cod ...
- CodeForces 149D Coloring Brackets
Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...
- CentOS 编译安装 MySQL5.7
下载 所有版本下载地址: http://dev.mysql.com/downloads/mysql/ 此处用 5.7.10 wget http://dev.mysql.com/get/Download ...
- 我所了解的WEB开发 (1)
开始接触网站开发的时候,概念里就对静态网站和动态网站有了简单的区分,静态网站仅仅是纯粹的HTML网页,动态网站是需要采用asp 连接数据库(比如access).那个时候听说高手都是使用 Notepad ...