Path: csiph.com!usenet.pasdenom.info!goblin1!goblin.stu.neva.ru!newsfeed.pionier.net.pl!news.dialog.net.pl!not-for-mail From: Arivald Newsgroups: comp.lang.java.programmer Subject: Re: Exception Handling Date: Sun, 11 Mar 2012 20:34:40 +0100 Organization: Dialog Net Lines: 76 Message-ID: References: NNTP-Posting-Host: dynamic-78-8-2-138.ssp.dialog.net.pl Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-2; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.dialog.net.pl 1331494611 16547 78.8.2.138 (11 Mar 2012 19:36:51 GMT) X-Complaints-To: abuse@dialog.net.pl NNTP-Posting-Date: Sun, 11 Mar 2012 19:36:51 +0000 (UTC) User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20120229 Thunderbird/11.0 In-Reply-To: X-Antivirus: avast! (VPS 120311-0, 2012-03-11), Outbound message X-Antivirus-Status: Clean Xref: csiph.com comp.lang.java.programmer:12869 W dniu 2012-03-11 18:05, Novice pisze: > Lew 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