The public void Printstacktrace () outputs this throwable and its traces 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. The first line of the output contains the result of the toString () method of this object. The remaining rows represent data previously recorded by Method Fillinstacktrace (). The format of this information depends on the implementation, but the following examples are the most common:
Java.lang.NullPointerException
At Myclass.mash (myclass.java:9)
At Myclass.crunch (myclass.java:6)
At Myclass.main (Myclass.java:3)
This example builds by running the following program:
Class MyClass {
public static void Main (string[] args) {
Crunch (null);
}
static void Crunch (int[] a) {
Mash (a);
}
static void Mash (int[] b) {
System.out.println (B[0]);
}
}
And there is,
catch (Exception e) {
E.printstacktrace ();
}
When an exception occurs in a try statement, the statements in the catch are executed, and the Java Runtime system automatically initializes the exception e in the catch bracket, which is the object that instantiates the exception type. E is the name of this object reference. Then e (reference) automatically calls the method specified in the exception class, and E.printstacktrace () appears.
The Printstacktrace () method means that you print the location of the exception information in the program and the cause of the error in the command line. (It's a vernacular explanation, it's easier to understand)
About System.out.println (e) and E.printstacktrace ()
In the case of throwing an exception, there are a number of methods, SYSTEM.OUT.PRINTLN (e); This method prints out the exception, and the output where the exception occurred, E.printstacktrace () also prints out the exception, but it also shows a deeper invocation information. For example:
A extends---> B extends---> C
We throw an exception when a problem occurs during the creation of a. <
System.out.println (e), except for standard exceptions, print at A and then output to the outer layer.
E.printstacktrace (), except for standard exceptions, print
At C
At B
At A
....... Then investigate the outer layers.
In the case of investigation into the outer layer, the same is true. I'll be back at Com.sun.midp.main.Main.main.
System.out.println (); When an exception occurs, display your own set of string information, such as the following code, try inside the IO operation, you know to throw an exception is definitely an IO exception, the catch is written on a specific IOException, You can print it yourself.
try{
function
}
catch (IOException exc) {
System.out.println ("IOException");
}
E.printstacktrace (); Is the stack information for printing exceptions, indicating the cause of the error, in fact, when an exception occurs, usually to deal with the exception, which is a good habit of programming, so E.printstacktrace () can be convenient for you to debug the program!
Java about E.printstacktrace () introduction