The role of Stringwriter/printwriter in Java output exception information

Source: Internet
Author: User

Nothing to do, look at the source code of JUnit. Just beginning to see a piece of interesting code:

Public String Trace () {        new StringWriter ();        new PrintWriter (StringWriter);        Thrownexception (). Printstacktrace (writer);        StringBuffer buffer = Stringwriter.getbuffer ();        return buffer.tostring ();    }  

I had not contacted StringWriter and PrintWriter before. Curious about it. In fact, Java IO is more complex, because many classes provide interfaces require the IO parameter type is fixed, and we know the data or need to enter the data is a lot of package types, and therefore often need to do the encapsulation work. (Personal little experience, I do not have much experience in IO piece)

Check out the Java API documentation and discover:

Voidprintstacktrace () and its backtrace to the standard           error stream. Voidprintstacktrace (PrintStream s)           Print stream. Voidprintstacktrace (printwriter s)           print writer.  

As can be seen from the above information, Throwable (exception inherited a base class) error input has three, printstacktrace () refers to the exception itself and the exception information output to the standard error stream; Printstatcktrace ( PrintStream s) refers to the output of the exception itself and the exception information to the PrintStream object, and the third is output to PrintWriter.

In the ordinary case, if we use the IDE, the error is usually output directly to the console, but sometimes we need to output the exception information to the file, or other pages, this time we need to use the parameters of the two API interface.

Here we use Printwriter,printwriter's constructors to compare a variety, and I use StringWriter as a construction parameter.

To prove the feasibility. Write a small program to run.

Import Java.io.PrintWriter; import Java.io.StringWriter; @SuppressWarnings ("serial")new PrintWriter (SW); Printstacktrace (PW);  return sb.tostring (); }}
PublicClasstestexception {  PublicStaticvoid Main (string[] args) {   try {   span class= "keyword" >throw new myexception ();   catch (myexception e) {  Span class= "indent" > //because the implemented method is defined in MyException, the catch parameter cannot be transformed upward to exception    System.out.println (    //e.printstacktrace ();  }}}        /span>                 

When using the E.printstacktrace () method, the following results are obtained:

MyException
At Testexception.main (testexception.java:4)

When using the method we define:

I am not an average exception:myexception
At Testexception.main (testexception.java:4)

Related code: https://github.com/louiscai/LocalRepository/tree/master/ExceptionMess

====================== Body End ==================

In addition

1) Throwable itself also has a getstacktrace () method.

2) regarding the difference between PrintWriter and PrintStream, you can refer to: http://hi.baidu.com/shdren09/item/8b1d2631e78b7abf623aff3f

The role of Stringwriter/printwriter in Java output exception information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.