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


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

Re: Is this the way to work with try with resources

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.java.programmer
Subject Re: Is this the way to work with try with resources
Date 2017-08-09 06:49 +0200
Organization Decebal Computing
Message-ID <87a839wdjs.fsf@munus.decebal.nl> (permalink)
References <87poc6wb9n.fsf@munus.decebal.nl> <refactor-20170808133822@ram.dialup.fu-berlin.de> <omcsep$org$1@newsreader4.netcologne.de>

Show all headers | View raw


Patrick Roemer <sangamon@netcologne.de> writes:

> Responding to Stefan Ram:
>> Cecil Westerhof <Cecil@decebal.nl> writes:
>>>It feels a bit long-winded.
>> 
>>   Yes. The method is too long. We need to refactor.
>
> Up to here we agree. ;)
>
> I'd rather move towards something like this:
>
> <snip>
> private static void statistics(Connection conn) {
>   try (Statement stmt = conn.createStatement()) {
>     String format = runQueryFun(stmt, COUNT_TOTAL_PROV,
>         rs -> totalStats(rs.getInt("no")));
>     runQueryProc(stmt, COUNT_UNUSED_PROV,
>         rs -> unusedStats(format, rs.getInt("no")));
>     runQueryProc(stmt, COUNT_USED_PROV,
>         rs -> usedStats(format, rs.getInt("no")));
>   }
>   catch (SQLException e) {
>     System.err.printf("Error in statistics\n");
>     e.printStackTrace();
>     fatalError(""); // wat?!
>   }
> }
> </snip>

Does not completely what I did, but I found a solution that does
something like this. See my reply on my own post.

It is just a desktop application. So I have a method fatalError which
prints an error if necessary and ‘cleanly’ exists the application.
    private static void fatalError(String message) {
        if (!message.equals("")) {
            System.err.println(message);
        }
        deinit(1);
    }

And the deinit method:
    private static void deinit(int exitCode) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                System.err.printf("deinit: Could not close the connection\n");
            }
            conn = null;
        }
        System.exit(exitCode);
    }

I know that in principle the connection does not need to be closed,
but I prefer to do it.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

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


Thread

Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-08 13:26 +0200
  Re: Is this the way to work with try with resources Martin Gregorie <martin@address-in-sig.invalid> - 2017-08-08 12:51 +0000
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:10 +0200
  Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-08 18:29 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:20 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:16 +0200
  Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-08 19:30 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 06:49 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:01 +0200
    Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-09 15:39 +0200
  Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-08 21:25 -0400
    Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-08 21:36 -0400
      Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:29 +0200
        Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 20:53 -0400
          Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 23:03 -0400
    Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-08 21:48 -0400
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:24 +0200
  Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 06:26 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 08:30 +0200
    Re: Is this the way to work with try with resources Andreas Leitgeb <avl@logic.at> - 2017-08-09 09:40 +0000
      Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 12:56 +0200
        Re: Is this the way to work with try with resources Andreas Leitgeb <avl@logic.at> - 2017-08-09 16:51 +0000
          Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 19:37 +0200
            Re: Is this the way to work with try with resources Andreas Leitgeb <avl@logic.at> - 2017-08-09 19:51 +0000
              Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 22:20 +0200
                Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 23:17 +0200
          Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-09 22:08 +0200
        Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 18:07 -0400
    Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 18:05 -0400
  Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 06:39 +0200

csiph-web