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


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

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

Newsgroups comp.lang.java.help
Subject Re: Good practice or not to close the file before System.exit(1)?
References <87mxjjqmlf.fsf@merciadriluca-station.MERCIADRILUCA>
From Merciadri Luca <Luca.Merciadri@student.ulg.ac.be>
Organization ULg
Date 2011-04-22 13:56 +0200
Message-ID <87ipu67b41.fsf@merciadriluca-station.MERCIADRILUCA> (permalink)

Show all headers | View raw


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The fact is that when some specific values of variables are
encountered, the program can't continue. I would have preferred
throwing an exception, but there is apparently no reason to throw an
exception because the variables that need to be checked come directly
from the arguments that the user passed to the JVM:

java myprogram 1 2 3

It it thus not an `internal error' when some values are encountered,
because they come from the user.

As a result, I don't throw an exception when some specific values of
the variables are encountered, but I make a System.err.println("Some
text") followed by a System.exit(1).

If everything is right (i.e. variables' values are correct) I then
need to close the file, but need to open again the file to do some
other things. As a result, the double opening is inside *one* try
block, so that I only need one catch.

I could put the inputFile.close() in a finally block, but as
inputFile.close() is useless (i.e. not executed) if System.exit(1) is
executed before, I would need to put System.exit(1) in the finally
block. But the problem is that as finally will always be executed,
System.exit(1) will always be executed (even if variables have no
problem), so that the program will always end. But I need to end only
if there was some problem with the variables' values!

I could also make two try blocks: one for the first opening, and one
for the second opening. But as only the first one will maybe cause
troubles, the only one that would need to be catched would be the
first try block. Or I would need to put exactly the same catch after
the second try, which would be ugly. As a result, I don't know what to
do.

- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

If you fake it, you can't make it.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAk2xbP4ACgkQM0LLzLt8MhyAwgCgpgnnoqwWK+QuQ01OEdt2l34J
izEAn1whb0xGrwJIsJg6zePodvWTjmiB
=G6Oa
-----END PGP SIGNATURE-----

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