Working with Exceptions |
You'll want to createException
subclasses when you need to handle errors specific to your own applications. You can make yourException
subclasses locale-independent by overriding theirgetLocalizedMessage
methods. EachgetLocalizedMessage
method you override should isolate the message it returns in aResourceBundle
. This allows the message to be translated into various languages during localization.In the example that follows, we'll show you how to create an
Exception
subclass that displays a locale-independent message. The source code for the example program is in two files: OverLimitException.java and Account.java.The
Account
program simulates a simple credit account. If this program exceeds the credit limit, it retrieves a localized message from aResourceBundle
calledExceptionBundle
, and then displays the message. In theExceptionBundle_en_US.properties
file, we specify this message as follows:Thepattern = Negative balance of {0,number,currency} is not allowed.Account
program fetches the message with thegetLocalizedMessage
method, which we implement in anException
subclass calledOverLimitException
. ThegetLocalizedMessage
method accepts aLocale
object as a parameter. We specify thisLocale
when fetching the message pattern from theResourceBundle
, and also when defining theMessageFormat
object. Therefore, ourOverLimitException
class is locale-sensitive:In thepublic class OverLimitException extends Exception { private double detail; public OverLimitException (double amount) { detail = amount; } public String getMessage() { return getLocalizedMessage(Locale.getDefault()); } public String getLocalizedMessage(Locale currentLocale) { ResourceBundle messages = ResourceBundle.getBundle("ExceptionBundle",currentLocale); Object[] messageArguments = {new Float(detail)}; MessageFormat formatter = new MessageFormat(""); formatter.setLocale(currentLocale); formatter.applyPattern(messages.getString("pattern")); return formatter.format(messageArguments); } }Account
class, thewithdraw
method throws anOverLimitException
if the new balance exceeds the credit limit. In the code that follows, note that we pass thevalue
parameter to theOverLimitException
constructor in thethrow
statement. The constructor assigns this parameter to thedetail
field of theOverLimitException
class. ThegetLocalizedMessage
method inserts aString
representation of thedetail
field in the message it returns. Thewithdraw
method is as follows:In thepublic void withdraw(double amount) throws OverLimitException { double value = balance - amount; if (value < creditLimit) { throw new OverLimitException(value); } else { balance = value; } }main
method of theAccount
class, we invoke thewithdraw
method and catch theOverLimitException
whenever it's thrown. Thecatch
clause prints theString
returned bygetLocalizedMessage
. Here is themain
method:Thestatic public void main(String[] args) { Locale[] locales = { new Locale("en","US"), new Locale("de","DE") }; Account credit = new Account(); credit.deposit(20.00f); for (int k = 0; k < locales.length; k++) { try { credit.withdraw(1000.00f); } catch (OverLimitException e) { System.out.println("Locale: " + locales[k].toString()); System.out.println(e.getLocalizedMessage(locales[k])); } } // for }Account
program displays two localized messages. Not only is the message text in the correct language, but the currency is expressed in a format appropriate to eachLocale
:% java Account Locale: en_US Negative balance of ($980.00) is not allowed. Locale: de_DE Ein negativer Kontostand von -980,00 DM is nicht gestattet.
Working with Exceptions |