Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #37135

Re: Where to put the return

Newsgroups comp.lang.java.programmer
Date 2017-08-11 08:31 -0700
References <87zib6zeup.fsf@munus.decebal.nl>
Message-ID <bcb6944e-0149-48f9-8007-22cfe07fbc8f@googlegroups.com> (permalink)
Subject Re: Where to put the return
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


On Friday, August 11, 2017 at 4:44:19 AM UTC-4, 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?
> 

It depends what you're trying to return.  If you do it like that, declare a simple variable to be returned before the try block, there's no reason the return needs to be in the try block.  If you have a method which could throw an error as the parameter to the return then of course it needs to be in a try block.  If the return is after the try block you need to know if it's acceptable to return a value if the method catches an exception.  If not, you need to remember to throw something in the catch clause regardless.  It should compile with the return in the try block as long as there is a return or throw in the catch clause, or after the try block which would work the same as putting it in the catch clause unless the return in the try block is in a conditional statement.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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