Throwable getMessage() method in Java with Examples
The getMessage() method of Throwable class is used to return a detailed message of the Throwable object which can also be null. One can use this method to get the detail message of exception as a string value.
Syntax:
public String getMessage()
Return Value: This method returns the detailed message of this Throwable instance.
Below programs demonstrate the getMessage() method of java.lang.Throwable Class
Example 1:
// Java program to demonstrate // the getMessage() Method. import java.io.*; class GFG { // Main Method public static void main(String[] args) throws Exception { try { // divide the numbers divide(2, 0); } catch (ArithmeticException e) { System.out.println("Message String = " + e.getMessage()); } } // method which divide two numbers public static void divide(int a, int b) throws ArithmeticException { int c = a / b; System.out.println("Result:" + c); } } |
Message String = / by zero
Example 2:
// Java program to demonstrate // the getMessage() Method. import java.io.*; class GFG { // Main Method public static void main(String[] args) throws Exception { try { test(); } catch (Throwable e) { System.out.println("Message of Exception : " + e.getMessage()); } } // method which throws UnsupportedOperationException public static void test() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } } |
Message of Exception : null
References:
https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#getMessage()
Recommended Posts:
- LogRecord getMessage() method in Java with Examples
- Throwable toString() method in Java with Examples
- Throwable getLocalizedMessage() method in Java with Examples
- Throwable setStackTrace() method in Java with Examples
- Throwable addSuppressed() method in Java with Examples
- Throwable getStackTrace() method in Java with Examples
- Throwable getSuppressed() method in Java with Examples
- Throwable getCause() method in Java with Examples
- Throwable printStackTrace() method in Java with Examples
- Throwable initCause() method in Java with Examples
- Throwable fillInStackTrace() method in Java
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.lang.Short toString() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



