StringWriter/PrintWriter在Java输出异常信息中的作用
闲来无事,看看JUnit的源代码。刚刚开始看就发现一段有趣的代码:
public String trace() {
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);
thrownException().printStackTrace(writer);
StringBuffer buffer = stringWriter.getBuffer();
return buffer.toString();
}
此前我并未接触过StringWriter和PrintWriter。对此感到好奇。其实Java的IO是比较复杂的,因为很多类提供的接口需要的IO参数类型是固定的,而我们掌握的数据或者说需要输入的数据却是很多封装类型的,故此经常需要做封装工作。(个人的小体会而已,我对IO一块没有太多经验)
查阅Java API文档,发现了:
void printStackTrace()
Prints this throwable and its backtrace to the standard error stream.
void printStackTrace(PrintStream s)
Prints this throwable and its backtrace to the specified print stream.
void printStackTrace(PrintWriter s)
Prints this throwable and its backtrace to the specified print writer.
从上面的信息可以看出,Throwable(Exception继承的一个基类)的错误输入有三种,printStackTrace()是指将异常本身和异常信息输出到标准的错误流;printStatckTrace(PrintStream s)是指将异常本身和异常信息输出到PrintStream的对象中;第三种则是输出到PrintWriter中。
在普通的情况中,如果我们用IDE的话,错误一般是直接输出到Console中,但是有时候我们需要将异常信息输出到文件中、或者是其他网页中,这时候就需要使用带参数的两个API接口。
此处使用PrintWriter,PrintWriter的构造函数比较多种,我使用StringWriter作为构造参数。
为了证实可行性。写一个小程序跑一下。
import java.io.PrintWriter;
import java.io.StringWriter; @SuppressWarnings("serial")
public class MyException extends Exception{
public String getPrintStackTraceAsString(){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
printStackTrace(pw);//将异常信息输入到pw(PrintWriter)中
StringBuffer sb = sw.getBuffer();
return sb.toString();
}
}
public class TestException {
public static void main(String[] args) {
try {
throw new MyException();
} catch (MyException e) {
// 由于实现的方法定义在MyException中,所以catch的参数不可以向上转型为Exception
System.out.println("I am not an average Exception: "
+ e.getPrintStackTraceAsString());
// e.printStackTrace();
}
}
}
当使用e.printStackTrace()方法时,得到以下结果:
MyException
at TestException.main(TestException.java:4)
而使用我们定义的方法时:
I am not an average Exception: MyException
at TestException.main(TestException.java:4)
此外:
1)Throwable本身也有getStackTrace()方法。
2)关于PrintWriter和PrintStream的区别,可以参考:http://hi.baidu.com/shdren09/item/8b1d2631e78b7abf623aff3f
StringWriter/PrintWriter在Java输出异常信息中的作用的更多相关文章
- 你真的会阅读Java的异常信息吗?
给出如下异常信息: java.lang.RuntimeException: level 2 exception at com.msh.demo.exceptionStack.Test.fun2(Tes ...
- Java输出错误信息与调试信息
创建一个类,在该类的main()主方法中,使用System类中的out和err两个成员变量来完成调试与错误信息的输出. public class PrintErrorAndDebug { public ...
- Java输出double类型中的最小正数和最大正数
这是<写给大忙人看的java核心技术>中的一道练习题. 1. 输出最大正数值 System.out.println(Double.MAX_VALUE); 直接输出包装类Double的MAX ...
- 解决Eclipse启动时报Initializing Java Tooling异常信息
1.启动Eclipse报错:An internal error occurred during: "Initializing Java Tooling".java.lang.Nul ...
- name属性作用+使用$.post()取代name属性在提交表单信息中的作用
name的用途 1)主要是用于获取提交表单的某表单域信息, 作为可与服务器交互数据的HTML元素的服务器端的标示,比如input.select.textarea.框架元素(iframe.frame. ...
- 在日志中记录Java异常信息的正确姿势
遇到的问题 今天遇到一个线上的BUG,在执行表单提交时失败,但是从程序日志中看不到任何异常信息. 在Review源代码时发现,当catch到异常时只是输出了e.getMessage(),如下所示: l ...
- Java:异常的处理
异常分两种大的异常类型,运行时异常和受检查异常. 用户既可以使用系统的异常类来处理异常信息,也可以创建系统的异常类的子类来自定义异常,这种方式比较灵活,虚拟机可以报出自己设置的异常信息,清楚明白. 1 ...
- JAVA基础——异常详解
JAVA异常与异常处理详解 一.异常简介 什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错.在java中,阻止当前方法或作用域的情况,称之为异常. java中异常的体系是怎么样的呢? 1 ...
- Java常见异常(Runtime Exception )小结(转)
原文链接:Java常见异常(Runtime Exception )小结 Java异常体系结构呈树状,其层次结构图如图 1所示: 本文重在Java中异常机制的一些概念.写本文的目的在 ...
随机推荐
- Java 容器一些知识
一.Collection 1.static 方法: Collections.sort(List<T>):实现List排序功能 Collections.fill(List<T> ...
- Android JNI的使用浅析
介绍JNI的好文章: http://blog.csdn.net/yuanzeyao/article/details/42418977 JNI技术对于多java开发的朋友相信并不陌生,即(java na ...
- genymotion模拟器配置Genymotion-ARM-Translation 兼容包
前提是你的adb的环境已经配置正确,不知道怎么配置的可参考http://jingyan.baidu.com/article/17bd8e52f514d985ab2bb800.html 如果还不成功的话 ...
- sql随机
想从MySQL数据库中随机取一条或者N条记录时,最好把RAND()生成随机数放在JOIN子查询中以提高效率. SELECT id FROM table ORDER BY RAND() LIMIT n; ...
- JQuery_图片未加载!
JQuery_图片未加载! <html> <head> <script type="text/javascript" src="/jquer ...
- position:fixed 居中问题
设置Css属性position:fixed后如何使这个盒子居中呢?其实也很简单: 就是需要设置给这个div盒子设置属性: left:0; right:0; margin:0 auto; ******* ...
- VBS一键配置VOIP脚本(其中包括VBS操作JS网页中的按钮事件--直接执行确认按钮中的脚本代码)
Dim ws,fso,IESet IE = WScript.createobject("InternetExplorer.Application")Set ws = WScript ...
- linux中iptables配置文件及命令详解详解
iptables配置文件 直接改iptables配置就可以了:vim /etc/sysconfig/iptables. 1.关闭所有的 INPUT FORWARD OUTPUT 只对某些端口开放. 下 ...
- Chapter 2 Open Book——2
It was worse because I was tired; 更糟糕的是因为我疲惫了. I still couldn't sleep with the wind echoing around t ...
- react中文API解读一(快速开始)
记下自己的react学习之路 ,官方文档写的很详尽,学起来应该比较简单 官方文档地址:react.http://reactjs.cn/react/docs/getting-started.html 1 ...