1. Overview
In this article, we will see the Java printstacktrace and its use case.
Before delving into the printstacktrace
method that is available in Throwable
class, let’s also recall our understanding on the Throwable
class.
2. Throwable
The Throwable
class is the superclass of all errors and exceptions available in the Java language.
Java Virtual Machine (JVM) can throw only the objects that are instances of this class or its subclasses. The same applies to the Java throw
statement as well.
Now, let’s talk about the printStackTrace
method.
3. Java printStackTrace
The printStackTrace is available as overloaded methods in the Java.lang.Throwable class. Below are its declarations:
- printStackTrace()
- void printStackTrace(PrintStream s)
- void printStackTrace(PrintWriter s)
These methods would print its Throwable class along with the backtrace information. The backtrace includes the details such as class name and line number of the exceptions for debugging and troubleshooting the issue.
It is very crucial to have these details so that you can easily detect which part of your code is causing the exception. Now, let’s see examples for each of these overloaded printStackTrace methods.
3.1. printStackTrace example
This method would also print its Throwable class along with the backtrace information to the standard error stream.
In the below code, we have our traditional divide a number by a zero code that would throw ArithmeticException
. Let’s also catch the exception and print it. To print, we are using the toString
method of the Exception
class to print the exception.
public class PrintStackTraceExample { public static void main(String args[]) { try { int x=0; int y=25; int z=25/0; } catch (Exception e) { System.out.println(e.toString()); /* prints java.lang.ArithmeticException: / by zero */ } System.out.println("End"); } }
The above code doesn’t print any line number or method name to troubleshoot the issue. Now, let’s replace the toString
method with the printStackTrace to print the exception.
The printStackTrace would print all necessary details such as the class, method, and the line number. Using these details, you can easily find the code causing the issue and fix it immediately.
public class PrintStackTraceExample { public static void main(String args[]) { try { int x=0; int y=25; int z=25/0; } catch (Exception e) { e.printStackTrace(); /* prints java.lang.ArithmeticException: / by zero at PrintStackTraceExample.main(PrintStackTraceExample.java:6) */ } System.out.println("End"); } }
If you see in the screenshot, the printStackTrace prints the details to the standard error stream.
3.2. Java printStackTrace(PrintStream s)
The only difference between the printStackTrace
and printStacktrace(PrintStream s)
is the latter prints the information to the specified PrintStream
.
In the below code, we are passing the PrintStream
"System.out"
to the printStackTrace
method as an argument. So it prints the information to the stream passed in the argument.
public class PrintStackTraceExample { public static void main(String args[]) { try { int x=0; int y=25; int z=25/0; } catch (Exception e) { e.printStackTrace(System.out); /* prints java.lang.ArithmeticException: / by zero at PrintStackTraceExample.main(PrintStackTraceExample.java:6) */ } System.out.println("End"); } }
If you notice, this printStackTrace takes the System.out
PrintStream
as argument and prints the details only to the passed System.out
stream.
3.2. Java printStackTrace(PrintWriter s)
The only difference between the printStackTrace
and printStacktrace(PrintWriter s)
is the latter prints the information to the specified
.PrintWriter
In the below code, we are initializing the PrintWriter
using the StringWriter
as a constructor argument. Then, we are passing the initialized PrintWriter
object to the printStackTrace
method.
All the details were written to the stringWriter
object. If you print it on the console, you can see all the details.
public class PrintStackTraceExample { public static void main(String args[]) { try { int x=0; int y=25; int z=25/0; } catch (Exception e) { StringWriter stringWriter = new StringWriter(); PrintWriter ps = new PrintWriter(stringWriter); e.printStackTrace(ps); System.out.println(stringWriter.toString()); /* prints java.lang.ArithmeticException: / by zero at PrintStackTraceExample.main(PrintStackTraceExample.java:6) */ } } }
You can also write to a file using the PrintWriter
object.
ps = new PrintWriter(new File("D:\\logs\\log.txt"));
4. Conclusion
To sum up, we have learned all the overloaded methods of printStackTrace along with examples.
Pingback: "Java PrintStackTrace" kodu Cevaplar - Kod Yanıtları
Pingback: "Java PrintStackTrace" Code de réponse - Coder les réponses
Pingback: java printstacktrace Code Example - java | Horje