Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37369
| From | Brixomatic <brixomatic@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Where to put the return |
| Date | 2017-08-31 19:27 +0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <MPG.3412656b6bff7099896c6@news.aioe.org> (permalink) |
| References | <87zib6zeup.fsf@munus.decebal.nl> |
In article <87zib6zeup.fsf@munus.decebal.nl>, Cecil Westerhof
(Cecil@decebal.nl) says...
>
> 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 personally like to keep the meat of the method together and variable
scope small.
I also don't think you're handling the case "no result" very well. For a
single result, an optional return value would probably be better. This
way the caller can handle the default case (and the caller probably
knows better what default is appropriate) and your error handling can be
more abstract.
So this would probably be my version, which I also find more readable:
private static Optional<String> getOldestUsed() {
try (Statement stmt = sqlite3Conn.createStatement();
ResultSet rsOldest = stmt.executeQuery(OLDEST_USED)) {
return rsOldest.next() ? Optional.of(rsOldest.getString());
: Optional.empty();
} catch (SQLException e) {
return failOptionalMethod("Database error", e);
}
}
private static <T> Optional<T> failOptionalMethod(String m,
Throwable t){
//1. optionally show an error to the user that includes a
// stacktrace that can be sent/copied to the support/bug tracker.
//2. log the error for your own statistics
return Optional.empty();
}
Kind regards,
Wanja
--
..Alesi's problem was that the back of the car was jumping up and down
dangerously - and I can assure you from having been teammate to
Jean Alesi and knowing what kind of cars that he can pull up with,
when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer]
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
Where to put the return Cecil Westerhof <Cecil@decebal.nl> - 2017-08-11 10:30 +0200
Re: Where to put the return Cecil Westerhof <Cecil@decebal.nl> - 2017-08-11 12:24 +0200
Re: Where to put the return Eric Douglas <e.d.programmer@gmail.com> - 2017-08-11 08:31 -0700
Re: Where to put the return Arne Vajhøj <arne@vajhoej.dk> - 2017-08-11 20:33 -0400
Re: Where to put the return Robert Klemme <shortcutter@googlemail.com> - 2017-08-13 00:27 +0200
Re: Where to put the return Arne Vajhøj <arne@vajhoej.dk> - 2017-08-12 21:41 -0400
Re: Where to put the return Robert Klemme <shortcutter@googlemail.com> - 2017-08-13 13:15 +0200
Re: Where to put the return Cecil Westerhof <Cecil@decebal.nl> - 2017-08-15 08:12 +0200
Re: Where to put the return Robert Klemme <shortcutter@googlemail.com> - 2017-08-19 11:10 +0200
Re: Where to put the return Cecil Westerhof <Cecil@decebal.nl> - 2017-08-19 12:54 +0200
Re: Where to put the return Cecil Westerhof <Cecil@decebal.nl> - 2017-08-19 13:46 +0200
Re: Where to put the return Cecil Westerhof <Cecil@decebal.nl> - 2017-08-19 14:10 +0200
Re: Where to put the return Robert Klemme <shortcutter@googlemail.com> - 2017-08-19 15:35 +0200
Re: Where to put the return Arne Vajhøj <arne@vajhoej.dk> - 2017-08-19 09:48 -0400
Re: Where to put the return Robert Klemme <shortcutter@googlemail.com> - 2017-08-19 23:26 +0200
Re: Where to put the return Arne Vajhøj <arne@vajhoej.dk> - 2017-08-19 20:01 -0400
Re: Where to put the return Robert Klemme <shortcutter@googlemail.com> - 2017-08-20 19:30 +0200
Re: Where to put the return Cecil Westerhof <Cecil@decebal.nl> - 2017-08-19 17:06 +0200
Re: Where to put the return Robert Klemme <shortcutter@googlemail.com> - 2017-08-19 15:35 +0200
Re: Where to put the return Arne Vajhøj <arne@vajhoej.dk> - 2017-08-19 09:43 -0400
Re: Where to put the return Cecil Westerhof <Cecil@decebal.nl> - 2017-08-19 17:15 +0200
Re: Where to put the return Robert Klemme <shortcutter@googlemail.com> - 2017-08-21 22:33 +0200
Re: Where to put the return lewbloch@gmail.com - 2017-08-19 09:20 -0700
Re: Where to put the return Robert Klemme <shortcutter@googlemail.com> - 2017-08-21 22:29 +0200
Re: Where to put the return Eric Douglas <e.d.programmer@gmail.com> - 2017-08-14 05:11 -0700
Re: Where to put the return Brixomatic <brixomatic@yahoo.com> - 2017-08-31 19:27 +0200
csiph-web