Throwable getLocalizedMessage() method in Java with Examples
Last Updated :
12 Apr, 2023
The getLocalizedMessage() method of Throwable class is used to get a locale-specific description of the Throwable object when an Exception Occurred. It helps us to modify the description of the Throwable object according to the local Specific message. For the subclasses which do not override this method, the default implementation of this method returns the same result as getMessage().
Syntax:
public String getLocalizedMessage()
Return Value: This method returns a locale-specific description of the Throwable object when an Exception Occurred.
Below programs demonstrate the getLocalizedMessage() method of Throwable Class:
Example 1:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
throws Exception
{
try {
addPositiveNumbers( 2 , - 1 );
}
catch (Exception e) {
System.out.println("LocalizedMessage = "
+ e.getLocalizedMessage());
}
}
public static void addPositiveNumbers( int a, int b)
throws Exception
{
if (a < 0 || b < 0 ) {
throw new Exception("Numbers are not Positive");
}
else {
System.out.println(a + b);
}
}
}
|
Output:
LocalizedMessage = Numbers are not Positive
Example 2:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
throws Exception
{
try {
testException();
}
catch (Throwable e) {
System.out.println("LocalizedMessage of Exception : "
+ e.getLocalizedMessage());
}
}
public static void testException()
throws IndexOutOfBoundsException
{
throw new IndexOutOfBoundsException(
"Forcefully Generated Exception");
}
}
|
Output:
LocalizedMessage of Exception : Forcefully Generated Exception
References: https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#getLocalizedMessage()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...