Re: Exception Handling
W dniu 2012-03-11 18:05, Novice pisze:
Lew<noone@lewscanon.com> wrote innews:jjh39f$crc$1@news.albasani.net:
Novice wrote:
snip
I have a utility class called LocalizationUtils which basically
houses convenience methods dealing with i18n/l10n. One of its methods
is getResources(). It expects two parameters, a String representing
the "base name" (the leading part of the resource file name) and a
locale.
Here's the code for getResources() with all comments and error
handling (aside from the empty catch block) stripped out:
====================================================================
static public ResourceBundle getResources(String baseName, Locale
locale {
ResourceBundle locList = null;
Don't use throwaway initializations, usually.
Why?
If I omit that line and simply use the following in the try block:
ResourceBundle locList = ResourceBundle.getBundle(baseName, locale);
locList isn't visible after the try/catch so that I can return it.
Simplest solution, skip variable at all.
static public ResourceBundle getResources(
String baseName, Locale locale) {
try {
return ResourceBundle.getBundle(baseName, locale);
} catch (MissingResourceException e) {
logger.error('missing resource', e);
return null;
}
}
or, if you need to do something on variable:
try {
ResourceBundle locList = ResourceBundle.getBundle(baseName, locale);
... do something ...
return locList;
} catch (MissingResourceException e) {
logger.error('missing resource', e);
return null;
}
or if you want exception block only for ResourceBundle.getBundle()
ResourceBundle locList; // no null assignment, simply uninitialized
try {
locList = ResourceBundle.getBundle(baseName, locale);
} catch (MissingResourceException e) {
logger.error('missing resource', e);
return null;
}
do something with locList ...
return locList;
--
Arivald