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


Groups > comp.lang.java.help > #579

Re: Good practice or not to close the file before System.exit(1)?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail
From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.help
Subject Re: Good practice or not to close the file before System.exit(1)?
Date Fri, 22 Apr 2011 09:39:09 -0400
Organization albasani.net
Lines 54
Message-ID <ios0da$ghh$1@news.albasani.net> (permalink)
References <87mxjjqmlf.fsf@merciadriluca-station.MERCIADRILUCA> <v1e1r6h1no1rjhsuit8cf7rfpkh7b59889@4ax.com> <finally-20110422035708@ram.dialup.fu-berlin.de> <k4q2r6hhm9khdm84c9qncmc2j0430dtikh@4ax.com> <CqOdnTCcLoBr8izQnZ2dnUVZ_uOdnZ2d@earthlink.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.albasani.net Ng0hgxzXYPo9A7eaY0EEks4AfdbS+e3QHQsNfLLCJO4BqiX5o/ALSHyxBvxXVXTwC3/OsFtf7pS8J4avjB85AvUFJ56jgwgSKmFv9cGk8doI+F5InWvbkWylQytQlk/i
NNTP-Posting-Date Fri, 22 Apr 2011 13:38:50 +0000 (UTC)
Injection-Info news.albasani.net; logging-data="DPg/QfDN0YhAA1c4/g1HM2YJAftz40XGyfIXyAPE86fG/wiv1Pmk4LR9PvlobSllpE57/48CV6l1gP6l4qtVH756dMZn0qQ5H5otDQhFTGwPCA5prwjyl/SP3QvMlV2A"; mail-complaints-to="abuse@albasani.net"
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8
In-Reply-To <CqOdnTCcLoBr8izQnZ2dnUVZ_uOdnZ2d@earthlink.com>
Cancel-Lock sha1:2GvfDBqADmkUl2bhQ09yxKg498k=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.help:579

Show key headers only | View raw


Patricia Shanahan wrote:
> rossum wrote:
>> Good catch, my mistake. System.exit() bypasses any finally blocks.
>> Ouch.
>>
>> To the OP: don't use System.exit(), throw an exception instead.

> If the program should terminate with a non-zero status code, the OP has
> to call System.exit(). Exceptions can only reduce the number of
> different places where it is called, and put the calls at an appropriate
> level in the call hierarchy.

Melding all that good advice and cautionary warning into just one out of many 
possible valid approaches:

  BufferedReader reader = new BufferedReader( ... );
  // create assertable reader != null here

  Outcome outcome; // custom enum - assume necessary properties
  try
  {
    ...
    outcome = Outcome.SUCCESS;
  }
  catch ( IOException exc )
  {
    outcome = Outcome.SCREWUP;
    logger.error( outcome.toString(), exc );
  }
  finally
  {
    try
    {
      reader.close();
    }
    catch ( IOException exc )
    {
      logger.error( "Screwup in close()", exc );
    }
  }
  switch ( outcome )
  {
    case SCREWUP:
      System.exit( 1 );
      break;

    case SUCCESS:
      break;
  }

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Good practice or not to close the file before System.exit(1)? Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-22 00:13 +0200
  Re: Good practice or not to close the file before System.exit(1)? rossum <rossum48@coldmail.com> - 2011-04-22 00:11 +0100
    Re: Good practice or not to close the file before System.exit(1)? rossum <rossum48@coldmail.com> - 2011-04-22 12:39 +0100
      Re: Good practice or not to close the file before System.exit(1)? Patricia Shanahan <pats@acm.org> - 2011-04-22 05:09 -0700
        Re: Good practice or not to close the file before System.exit(1)? Lew <noone@lewscanon.com> - 2011-04-22 09:39 -0400
          Re: Good practice or not to close the file before System.exit(1)? Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-23 13:20 +0200
    Re: Good practice or not to close the file before System.exit(1)? Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-22 16:37 +0200
  Re: Good practice or not to close the file before System.exit(1)? Patricia Shanahan <pats@acm.org> - 2011-04-22 04:55 -0700
  Re: Good practice or not to close the file before System.exit(1)? Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-22 13:56 +0200
    Re: Good practice or not to close the file before System.exit(1)? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-04-22 09:36 -0300
    Re: Good practice or not to close the file before System.exit(1)? markspace <-@.> - 2011-04-22 08:20 -0700
      Re: Good practice or not to close the file before System.exit(1)? Patricia Shanahan <pats@acm.org> - 2011-04-22 09:04 -0700
        Re: Good practice or not to close the file before System.exit(1)? markspace <-@.> - 2011-04-22 15:24 -0700
          Re: Good practice or not to close the file before System.exit(1)? markspace <-@.> - 2011-04-22 18:11 -0700
            Re: Good practice or not to close the file before System.exit(1)? markspace <-@.> - 2011-04-23 08:25 -0700
      Re: Good practice or not to close the file before System.exit(1)? Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-22 18:55 +0200
        Re: Good practice or not to close the file before System.exit(1)? Lew <noone@lewscanon.com> - 2011-04-22 13:40 -0400
        Re: Good practice or not to close the file before System.exit(1)? Patricia Shanahan <pats@acm.org> - 2011-04-22 10:55 -0700
          Re: Good practice or not to close the file before System.exit(1)? Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-22 22:58 +0200
            Re: Good practice or not to close the file before System.exit(1)? Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-22 23:09 +0200
              Re: Good practice or not to close the file before System.exit(1)? Patricia Shanahan <pats@acm.org> - 2011-04-22 14:30 -0700
                Re: Good practice or not to close the file before System.exit(1)? markspace <-@.> - 2011-04-22 15:38 -0700
                Re: Good practice or not to close the file before System.exit(1)? Patricia Shanahan <pats@acm.org> - 2011-04-22 17:00 -0700
                Re: Good practice or not to close the file before System.exit(1)? Lew <noone@lewscanon.com> - 2011-04-22 22:10 -0400
                Re: Good practice or not to close the file before System.exit(1)? Patricia Shanahan <pats@acm.org> - 2011-04-22 19:46 -0700
                Re: Good practice or not to close the file before System.exit(1)? Lew <noone@lewscanon.com> - 2011-04-23 09:54 -0400
  Re: Good practice or not to close the file before System.exit(1)? Roedy Green <see_website@mindprod.com.invalid> - 2011-04-22 12:45 -0700
    Re: Good practice or not to close the file before System.exit(1)? Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-22 23:20 +0200

csiph-web