Path: csiph.com!news.redatomik.org!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: Where to put the return Date: Fri, 11 Aug 2017 20:33:51 -0400 Organization: Aioe.org NNTP Server Lines: 45 Message-ID: References: <87zib6zeup.fsf@munus.decebal.nl> NNTP-Posting-Host: PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 Content-Language: en-US X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:37137 On 8/11/2017 4:30 AM, Cecil Westerhof wrote: > I have the following function: > private static String getOldestUsed() { > String oldestUsed = ""; > > try (Statement stmt = sqlite3Conn.createStatement(); > ResultSet rsOldest = stmt.executeQuery(OLDEST_USED)) { > if (rsOldest.next()) { > oldestUsed = rsOldest.getString("used"); > } > } catch (SQLException e) { > giveError("Error in displayOldestUsed"); > e.printStackTrace(); > fatalError(""); > } > return oldestUsed; > } > > Often I see the return in the try block. But personally I prefer it > this way. Is there a preferred way, or are both ways acceptable? I would go for: private static final String NO_VALUE = ""; private static String getOldestUsed() { try (Statement stmt = sqlite3Conn.createStatement(); ResultSet rsOldest = stmt.executeQuery(OLDEST_USED)) { if (rsOldest.next()) { return rsOldest.getString("used"); } else { return NO_VALUE; } } catch (SQLException e) { giveError("Error in displayOldestUsed"); e.printStackTrace(); fatalError(""); return NO_VALUE; } } But there are many acceptable ways of writing this. Arne