Java Error Stack Trace
The stack trace in Java is an array of stacks.The stack trace reveals the console's location of an exception or error by gathering data from all program methods. The JVM displays the stack trace when an error or exception is thrown. The location of the exceptions is all that is meant by "stack trace" in Java.
To put it another way, we can say that stack trace looks for (traces) the next line where an exception might occur. The stack trace in Java consists of an array of stack frames.Stack backtrace (or backtrace) is another name for it.The stack frames depict the application's movement during program execution.
It records the places where exceptions were raised. It gathers information about every method a program calls. Java stack trace and prints the stack trace on the console by default when we don't care about unhandled exceptions, and the program throws exceptions.When an exception is thrown, the JVM generates the stack trace independently. Each element in the stack trace is a method call. You can use the printSatckTrace() method of the Java Throwable classto print the stack trace to the console Stack traces are now contained in an array of the Java StackTraceElement classintroduced in Java 1.5. An array returned by the getStackTrace() method of the Throwable class.
Each element is represented by one stack frame. Method calls appear in all stack frames except the first frame at the top. The execution point used by the JVM to generate the stack trace is shown in the first frame. The constructor of the StackTraceElement class takes four parameters as arguments and creates a stack trace element that marks the specified execution point.
- public StackTraceElement(String declaring class, String methodName, String fileName, int lineNumber)
Parameters:
- Declaring classes: The qualified name of the class that contains the execution point.
- MethodName: It represents the method name that contains the execution point.
- FileName: It represents the file name that contains the execution point.
- LineNumber: It represents the line number of the source of the execution point.
It throws the NullPointerException if the parameters declaring class and methodName are null.
You can print exceptions using a method of this data structure called printstackTrace(). Used to handle single exceptions in exception handlers. Java's Throwable classprovides methods to obtain a Throwable exception object, along with additional information such as theline number and class name of the exception. The superclass of all exception classes is throwable.
Below is a description of both stack traces.
- Single-line e-stack trace
- Multiple line stack trace
Single Line Stack Trace
Most generic exceptions fall in this category.
There are several exceptions, but for the implementation part, ‘ArrayIndexOutOfBound‘to be considered for implementation.
// Importing Classes/Files
import java.io.*;
class GFG {
// Main Driver Method
public static void main(String[] args) {
// Inserting elements into array
int a[] = { 1, 2, 3 };
try {
// Exception occurs
System.out.println(a[5]); }
// Try-Catch Block
catch (ArrayIndexOutOfBoundsException e) {
// Printing Exception Object as well as
// the line where Exception occur
e.printStackTrace();
}
}
Output:
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 3
Multi–Line Stack Trace
This condition occurs when we call the function inside into another function and if any function throws an Exception, retrace all the paths where it calls with the printstackTrace() Method.
// Importing Classes/Files
import java.io.*;
class GFG {
// Creating random function to test
static void check2(){
// Try-catch block for exception
try {
int a = 5 / 0;
//Exceptions occur as logically
// In math '/' operator satisfies x/y where y!=0 }
// Catch Block to catch an exception if occurs
catch (Exception e) {
// retrace all the paths where this function call
e.printStackTrace(); } }
// calling the function check2()
static void check() { check2(); }
// Driver Main Method
public static void main(String[] args)
{
check(); // calling the function check()
}
}
Output:
Java.lang.ArithmeticException: / by zero
The stack execution point is at line 1 of the stack trace, and the stack frames that make up the entire trace can be found fromline 2 to the end. You can see that the first method executed is the last stack frame in the stack trace, and the last method is the first stack frame. As a result, a stack frame is represented by each component of the stack trace.