|
|
Working with Exceptions |
You'll want to createExceptionsubclasses when you need to handle errors specific to your own applications. You can make yourExceptionsubclasses locale-independent by overriding theirgetLocalizedMessagemethods. EachgetLocalizedMessagemethod 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
Exceptionsubclass that displays a locale-independent message. The source code for the example program is in two files: OverLimitException.java and Account.java.The
Accountprogram simulates a simple credit account. If this program exceeds the credit limit, it retrieves a localized message from aResourceBundlecalledExceptionBundle, and then displays the message. In theExceptionBundle_en_US.propertiesfile, we specify this message as follows:Thepattern = Negative balance of {0,number,currency} is not allowed.Accountprogram fetches the message with thegetLocalizedMessagemethod, which we implement in anExceptionsubclass calledOverLimitException. ThegetLocalizedMessagemethod accepts aLocaleobject as a parameter. We specify thisLocalewhen fetching the message pattern from theResourceBundle, and also when defining theMessageFormatobject. Therefore, ourOverLimitExceptionclass 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); } }Accountclass, thewithdrawmethod throws anOverLimitExceptionif the new balance exceeds the credit limit. In the code that follows, note that we pass thevalueparameter to theOverLimitExceptionconstructor in thethrowstatement. The constructor assigns this parameter to thedetailfield of theOverLimitExceptionclass. ThegetLocalizedMessagemethod inserts aStringrepresentation of thedetailfield in the message it returns. Thewithdrawmethod 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; } }mainmethod of theAccountclass, we invoke thewithdrawmethod and catch theOverLimitExceptionwhenever it's thrown. Thecatchclause prints theStringreturned bygetLocalizedMessage. Here is themainmethod: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 }Accountprogram 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 |