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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

LocalizedMessage = Numbers are not Positive

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the ensureCapacity() 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");
    }
}

chevron_right


Output:

LocalizedMessage of Exception : Forcefully Generated Exception

References:
https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#getLocalizedMessage()



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.