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


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

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> <87ipu67b41.fsf@merciadriluca-station.MERCIADRILUCA> <ios6c3$dpq$1@dont-email.me>
From Merciadri Luca <Luca.Merciadri@student.ulg.ac.be>
Organization ULg
Date 2011-04-22 18:55 +0200
Message-ID <87bozy9qek.fsf@merciadriluca-station.MERCIADRILUCA> (permalink)

Show all headers | View raw


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

markspace <-@.> writes:

> On 4/22/2011 4:56 AM, Merciadri Luca wrote:
>> -----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
>
> Doesn't matter, throw an exception.
>
>>
>> It it thus not an `internal error' when some values are encountered,
>> because they come from the user.
>
> Doesn't matter.  Exceptions are their for you to extend and use for
> your own purposes.  They are not limited to a few internal errors but
> cover all types of exceptional processing.
>
>>
>> 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).
>
> Compare with the Apache CLI package, which does the grunt work of
> reading command line arguments for you:
>
> parse
>
> CommandLine parse(Options options, String[] arguments)
>                   throws ParseException
>
> They make their own exception, ParseException, and throw it when they
> encounter unknown flags or other errors.  The expectation is that you
> will catch ParseException, print out a usage line, preform any other
> final processing (close files), and then exit with status.
>
> <http://commons.apache.org/cli/usage.html>
>
> In your particular case, yes you could almost certainly avoid opening
> the files first.  However in general this follows the same pattern
> that all other exceptional processing follows:  throw an error when
> you have a problem, let the high level code catch it and deal with the
> results.

Okay. I might switch to exceptions for dealing with user input,
then. I was dubitative because of so many answers about this... For
example, at
<http://groups.google.com/group/comp.lang.java.help/browse_thread/thread/8f3a236c4ff5a87e/4be4e8faf89e1b52?show_docid=4be4e8faf89e1b52>,
Mr. Green tells me that exit should be used for application data/user
errors. Surely exiting with System.exit won't give you any stack dump,
but my idea was the following: does the user really need a stack dump
when (s)he knows that he's given bad values to your program?

I think I'll use exceptions, though.

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

Fall down seven times, stand up eight. 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAk2xsxMACgkQM0LLzLt8Mhz+SACgpoh0SGOmgj6WNmry4vRVe4wM
I/kAn2Lsxr4RFFGWBYeF9V1PM4rAadbc
=jA1b
-----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