'Difference between e.getMessage() and e.getLocalizedMessage()

What is the difference between the getMessage and getLocalizedMessage methods on Java exceptions?

I am using these both methods to get the message thrown by the catch block while performing error handling. Both of them get me the message from the error handling, but how exactly do these two differ?

I did some searching on the Internet and found this answer on Answers.com:

Java Exceptions inherit their getMessage and getLocalizedMessage methods from Throwable (see the related link). The difference is that subclasses should override getLocalizedMessage to provide a locale-specific message.

For example, imaging that you're adapting code from an American-English speaking company/group to a British-English group. You may want to create custom Exception classes which override the getLocalizedMessage to correct spelling and grammer to what the users and developers who will be using your code might expect. This can also be used for actual translations of Exception messages.


Questions:

  • Does that mean language specific implementations? Like if I use e.getLocalizedMessage() for example my app in English - error will be thrown in English, if I use my app in Spanish - then error will be thrown in Spanish?

  • Need some clear explanation on where and when I can use these methods to my use/



Solution 1:[1]

It is really surprising - Check the openJDK 7 code of Throwable.java class.

Implementation of getLocalizedMessage is -

390     public String getLocalizedMessage() {
391         return getMessage();
392     }

And implemenation of getMessage is -

376     public String getMessage() {
377         return detailMessage;
378     }

And

130     private String detailMessage;

There is no change in implemenation of both method but documentation.

Solution 2:[2]

no. it definitely does not mean language specific implementations. it means implementations that use an internationalization (aka i18n) mechanism. see this page for more details of what resource bundles are and how to use them.

the gist is that you place any text in resource files, of which you have many (one per locale/language/etc) and your code uses a mechanism to look up the text in the correct resource file (the link i provided goes into details).

as to when and where this gets used, its entirely up to you. normally you'd only be concerned about this when you want to present an exception to a non-technical user, who may not know english that well. so for example, if you're just writing to log (which commonly only technical users read and so isnt a common i18n target) you'd do:

try {
   somethingDangerous();
} catch (Exception e) {
   log.error("got this: "+e.getMessage());
}

but if you intend to display the exception message to the screen (as a small dialogue, for example) then you might want to display the message in a local language:

try {
   somethingDangerous();
} catch (Exception e) {
   JOptionPane.showMessageDialog(frame,
    e.getLocalizedMessage(),
    "Error",  <---- also best be taken from i18n
    JOptionPane.ERROR_MESSAGE);
}

Solution 3:[3]

public String  getMessage() 

Returns the detail message string of this throwable.

public String getLocalizedMessage() 

Creates a localized description of this throwable. Subclasses may override this method in order to produce a locale-specific message. For subclasses that do not override this method, the default implementation returns the same result as getMessage().

In your case e is nothing but the object of exception ...

getLocalizedMessage() u need to override and give your own message i.e the meaning for localized message.

For example ... if there is a null pointer exception ...

By printing e it will display null

e.getMessage() ---> NullPointerException

Solution 4:[4]

This is what the Throwable.java class have to say. Maybe it can help someone:

/**
 * Returns the detail message string of this throwable.
 *
 * @return  the detail message string of this {@code Throwable} instance
 *          (which may be {@code null}).
 */
public String getMessage() {
    return detailMessage;
}

/**
 * Creates a localized description of this throwable.
 * Subclasses may override this method in order to produce a
 * locale-specific message.  For subclasses that do not override this
 * method, the default implementation returns the same result as
 * {@code getMessage()}.
 *
 * @return  The localized description of this throwable.
 * @since   JDK1.1
 */
public String getLocalizedMessage() {
    return getMessage();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Subhrajyoti Majumder
Solution 2
Solution 3 Lavekush Agrawal
Solution 4 sivi