Friday, February 20, 2026
HomeLanguagesJavaThrowable getLocalizedMessage() method in Java with Examples

Throwable getLocalizedMessage() method in Java with Examples

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




// Java program to demonstrate
// the getLocalizedMessage() Method.
 
import java.io.*;
 
class GFG {
 
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
 
        try {
            // add the numbers
            addPositiveNumbers(2, -1);
        }
        catch (Exception e) {
            System.out.println("LocalizedMessage = "
                               + e.getLocalizedMessage());
        }
    }
 
    // method which adds two positive number
    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




// Java program to demonstrate
// the getLocalizedMessage() Method.
 
import java.io.*;
 
class GFG {
 
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
 
        try {
            testException();
        }
 
        catch (Throwable e) {
            System.out.println("LocalizedMessage of Exception : "
                               + e.getLocalizedMessage());
        }
    }
 
    // method which throws IndexOutOfBoundsException
    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()

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS