🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The Throwable.getMessage() method in Java is used to retrieve the detail message string of the throwable. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.
Table of Contents
- Introduction
getMessage()Method Syntax- Understanding
getMessage() - Examples
- Basic Usage
- Custom Exceptions
- Real-World Use Case
- Conclusion
Introduction
The Throwable.getMessage() method returns the detail message string of the throwable, which provides more information about the error or exception that occurred. This message can be useful for debugging and logging purposes.
getMessage() Method Syntax
The syntax for the getMessage() method is as follows:
public String getMessage()
Parameters:
- This method does not take any parameters.
Returns:
- The detail message string of the throwable instance, or
nullif no message is available.
Understanding getMessage()
The getMessage() method retrieves the detail message string that was provided when the throwable (such as an exception or error) was created. This message is typically set in the constructor of the throwable and provides additional context about the error.
Examples
Basic Usage
To demonstrate the basic usage of getMessage(), we will create a simple example where an exception is thrown and its message is retrieved.
Example
public class GetMessageExample {
public static void main(String[] args) {
try {
throw new Exception("This is a basic exception message.");
} catch (Exception e) {
System.out.println("Exception message: " + e.getMessage());
}
}
}
Output:
Exception message: This is a basic exception message.
Custom Exceptions
You can create custom exceptions with specific messages and use the getMessage() method to retrieve those messages.
Example
public class CustomException extends Exception {
private static final long serialVersionUID = 1L;
public CustomException(String message) {
super(message);
}
}
public class CustomExceptionExample {
public static void main(String[] args) {
try {
throw new CustomException("This is a custom exception message.");
} catch (CustomException e) {
System.out.println("Custom exception message: " + e.getMessage());
}
}
}
Output:
Custom exception message: This is a custom exception message.
Real-World Use Case
Logging Exception Messages
In a real-world scenario, applications often need to log exception messages to help with debugging and error tracking. The getMessage() method provides a way to retrieve these messages for logging purposes.
Example
import java.io.FileWriter;
import java.io.IOException;
public class LoggingExceptionExample {
public static void main(String[] args) {
try {
throw new IOException("Failed to read the file.");
} catch (IOException e) {
logException(e);
}
}
public static void logException(Throwable throwable) {
try (FileWriter writer = new FileWriter("error.log", true)) {
writer.write("Exception occurred: " + throwable.getMessage() + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output in error.log:
Exception occurred: Failed to read the file.
Conclusion
The Throwable.getMessage() method in Java provides a way to retrieve the detail message string of a throwable. This method is useful for obtaining additional context about an error or exception, making it easier to debug and log issues in your application. Whether you are working with standard exceptions or custom exceptions, the getMessage() method offers a straightforward way to access error messages.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment