ide:netbeans8.0.2
Jdk:1.7.0_45
When we handle the exception, we catch the exception E and E.printstacktrace (). If you add standard output System.out.println Elsewhere, you will find that the console display does not display the information in the order in which we are outputting the code.
First I need to determine where the e.printstacktrace () will go, so the standard output is added to the inside and outside of the try block for validation.
See the following code:
public class Javatest {
private static void DoSomething () {
System.out.println ("1+1=2");
}
public static void Main (string[] args) {
file File=new file ("Test.txt");
FileInputStream Input=null;
try {
System.out.println ("before new");
Input=new fileinputstream (file);
System.out.println ("after new");
Input.close ();
} catch (Exception e) {
System.out.println ("before Printstacktrace");
E.printstacktrace ();
System.out.println ("after Printstacktrace");
}
finally{
System.out.println ("Finally block");
System.out.println ("Try Block finished");
DoSomething ();
}
So we're going to run this program now.
Get the following output:
Before new
before Printstacktrace
java.io.FileNotFoundException:test.txt (the system cannot find the specified file.) ) after
Printstacktrace
Finally block in
Java.io.FileInputStream.open (Native method)
try Block Finished
1+1=2 at
java.io.fileinputstream.<init> (fileinputstream.java:146) at
Javatest. Javatest.main (JAVATEST.JAVA:18)
It may also be the following:
Before new
Java.io.FileNotFoundException:test.txt (the system cannot find the specified file.)
before printstacktrace at
Java.io.FileInputStream.open (Native method) after
Printstacktrace
Finally block
Try blocks finished
1+1=2 at
java.io.fileinputstream.<init> (Fileinputstream.java : 146) at
Javatest. Javatest.main (JAVATEST.JAVA:18)
Of course, there are other things to try out.
From the above two kinds of operation can be found that the order of the output is not necessarily. From this conjecture, two kinds of output may not be an output at all, however, because of the operation of the operating system, the output of the information is rendered differently each time.
This way, referring to the jdk1.6.0 version of the API documentation, find the Printstacktrace () method of the exception class and see the first sentence of Printstacktrace () in its parent class Throwable:
Output this throwable and its trace to the standard error stream. This method outputs the stack trace of this Throwable object to the error output stream as the value of the field System.err.
As you can see, the printstacktrace is output through the System.err stream to the console, and the standard output we write is output to the console through the System.out stream. Both write to the console will receive the effect of operating system scheduling, the results of the above run.
Similar to the Stdin,stdout,stderr three standard streams in c\c++, the Java system.in,system.out,system.err corresponds to the above three standard streams.
Reference network information (without my verification)
If you want to separate the System.out from the System.err area, you need to redirect the System.out to another place, such as a file. Because the System.err cannot be redirected to a location other than the console, the console is used to display the System.err data, and the file displays the System.out data, and the information is not mixed together.