Last Updated : 06 Feb, 2019
The
getMessage()method of
Throwable classis 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
messageof this Throwable instance. Below programs demonstrate the getMessage() method of java.lang.Throwable Class
Example 1: Java
// 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);
}
}
Output:
Message String = / by zeroExample 2: Java
// 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();
}
}
Output:
Message of Exception : nullReferences: https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#getMessage()
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4