If you have a use case where you want your application to terminate with an exception but rather want to catch the exception and print the stack trace as a String, below is an example to demonstrate it.
package org.code2care.java.sorting.algo;
import java.io.PrintWriter;
import java.io.StringWriter;
public class ExceptionAsString {
public static void main(String[] args) {
try {
String string = null;
string.toString(); //Throws NPE
} catch (NullPointerException exception) {
StringWriter stringWriter = new StringWriter();
exception.printStackTrace(new PrintWriter(stringWriter));
String stackTrace = stringWriter.toString();
System.out.println(stackTrace);
}
}
}
java.lang.NullPointerException:
Cannot invoke "String.toString()" because "string" is null
at org.code2care.java.ExceptionAsString.main(ExceptionAsString.java:10)
Process finished with exit code 0
As you can see the exception was printed as a string and the program completed with an exit code of 0
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- How to Clear Gradle Cache on Mac - Gradle
- How to extend retiring SharePoint 2010 Workflows and continue with Office 365 - SharePoint
- How to rerun last command in Zsh shell - HowTos
- Program 1: Print Hello World! - 1000+ Python Programs - Python-Programs
- How to reset eclipse layout - Android
- How to save a file as csv in Windows Notepad? - Microsoft
- Fix MySQL ERROR 1045 (28000): Access denied for user root@localhost (using password: YES) - MySQL
- Create SharePoint Site Collection with new Content database in existing web application - SharePoint