获得java.lang.Throwable错误堆栈信息
2014-09-30 17:44
459 查看
在java语言中java.lang.Throwable是所有error、exception的父类。异常体系对于问题的查找、编码的实现都是非常有帮助的。
当你catch到一个异常Throwable e后,大家最常用的做法就是
如果你使用了log4j,那么我建议使用
如果你要将错误堆栈的系想你存储到数据库中,可以参考下面的类
最后,提一下debugg调试的时候,会遇到的一个奇怪的现象,java.lang.Throwable实例在调试时显示
当你catch到一个异常Throwable e后,大家最常用的做法就是
e.printStackTrace();
如果你使用了log4j,那么我建议使用
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; class Axxx { protected final Log logger = LogFactory.getLog(getClass()); { Throwable e; // ....... // ....... logger.error(e); } }
如果你要将错误堆栈的系想你存储到数据库中,可以参考下面的类
package com.mahh.jdk; import java.io.IOException; import java.io.LineNumberReader; import java.io.PrintWriter; import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * @author mahh * * @since:2013-1-28 上午10:06:40 * */ public class ThrowableInformation { protected final Log logger = LogFactory.getLog(getClass()); /**======================================================================= * 获得操作系统中,换行字符串。如Micorsoft Windows XP专业版中换行符"\r\n" * ======================================================================= */ // Note that the line.separator property can be looked up even by applets. // 参考org.apache.log4j.Layout public final static String LINE_SEP = System.getProperty("line.separator"); // 参考java.io.BufferedWriter public final static String LINE_SEP2 = (String) java.security.AccessController .doPrivileged(new sun.security.action.GetPropertyAction( "line.separator")); /** * @Description:将Throwable对象的错误堆栈内容形成字符串<br> 参考 * {@link org.apache.log4j.spi.ThrowableInformation} * 代码 * @param throwable * 异常对象 * @return * @author mahh * @since:2014-9-30 下午02:32:51 */ public static String[] getThrowableStrRep(Throwable throwable) { if (throwable == null) { return new String[0]; } StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); throwable.printStackTrace(pw); pw.flush(); LineNumberReader reader = new LineNumberReader(new StringReader( sw.toString())); ArrayList lines = new ArrayList(); try { String line = reader.readLine(); while (line != null) { lines.add(line); line = reader.readLine(); } } catch (IOException ex) { lines.add(ex.toString()); } String[] rep = new String[lines.size()]; lines.toArray(rep); return rep; } // 测试代码 public static void main(String[] args) { RuntimeException e1 = new RuntimeException("aaa"); RuntimeException e2 = new RuntimeException("e2", e1); RuntimeException e3 = new RuntimeException(e2); String[] errorStrArray = getThrowableStrRep(e3); for (int i = 0; i < errorStrArray.length; i++) { System.out.println(errorStrArray[i]); } } }
最后,提一下debugg调试的时候,会遇到的一个奇怪的现象,java.lang.Throwable实例在调试时显示
相关文章推荐
- Exception in thread "main" java.lang.NoClassDefFoundError错误信息
- 较少遇到的错误—严重: StandardWrapper.Throwable—java.lang.ArrayIndexOutOfBoundsException: 48188
- Java 中 手动抛出异常: throw new Exception("错误信息") 错误信息的获得
- java.lang.NullPointerException--jvm堆栈异常信息丢失问题
- java.lang.NullPointerException:null 没有打印出任何堆栈信息的解决办法
- Exception in thread "main" java.lang.NoClassDefFoundError错误信息
- java.lang.RuntimeException: Unable to start activity ComponentInfo 错误的一些信息
- Quartz定时器错误信息,java.lang.NoSuchMethodError: org.quartz.SchedulerException
- androidpn提示错误信息:java.lang.NoClassDefFoundError: org.androidpn.client.PersistentConnectionListener
- Android 错误信息: java.lang.StackOverflowError: stack size 8MB
- Exception in thread "main" java.lang.NoClassDefFoundError错误信息
- (转载)Exception in thread "main" java.lang.NoClassDefFoundError错误信息
- Quartz定时器错误信息,java.lang.NoSuchMethodError: org.quartz.SchedulerException
- 获得Java异常的堆栈信息
- Android 错误信息: java.lang.StackOverflowError: stack size 8MB
- 错误信息: java.lang.NoClassDefFoundError: javax/el/ExpressionFactory
- 错误信息:java.lang.NoSuchMethodError: javax.servlet.JSP.PageContext.getELContext()LJAVAx/el/ELContext
- Exception in thread "main" java.lang.NoClassDefFoundError错误信息
- 错误信息:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- elasticsearch client 为空 错误信息:java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor