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); } } } |
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"); } } |
LocalizedMessage of Exception : Forcefully Generated Exception
References: https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#getLocalizedMessage()