Thursday, September 4, 2025
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

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6746 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS