how to exit gracefully

Discussion in 'Java' started by mark jason, Dec 16, 2010.

  1. mark jason

    mark jason Guest

    hi,
    I have an application that expects a certain number of command line
    arguments.I need to check the number of command line args and if
    sufficient number of arguments are not provided ,would like to print
    the error message and exit.I have heard that using
    System.exit(somenumber) is frowned upon.Which is the best practice?
    regards
    mark.

    class MyApp{
    ....
    public static void main(String[] args){
    if (args.length< 4){
    System.out.println("Usage: java MyApp fname sname
    citycode statecode");
    System.exit(1);
    }
    String firstname = args[0];
    String surname = args[1];
    String cityCode = args[2];
    String stateCode = args[3];
    new MyApp().process(firstname, surname, cityCode, stateCode);
    ...
    }
    }
     
    mark jason, Dec 16, 2010
    #1
    1. Advertising

  2. "mark jason" <> wrote in message
    news:...
    > hi,
    > I have an application that expects a certain number of command line
    > arguments.I need to check the number of command line args and if
    > sufficient number of arguments are not provided ,would like to print
    > the error message and exit.I have heard that using
    > System.exit(somenumber) is frowned upon.Which is the best practice?
    > regards
    > mark.
    >
    > class MyApp{
    > ....
    > public static void main(String[] args){
    > if (args.length< 4){
    > System.out.println("Usage: java MyApp fname sname
    > citycode statecode");
    > System.exit(1);
    > }
    > String firstname = args[0];
    > String surname = args[1];
    > String cityCode = args[2];
    > String stateCode = args[3];
    > new MyApp().process(firstname, surname, cityCode, stateCode);
    > ...
    > }
    > }


    If main() will only be called when it's run as a command-line app, this is
    the best solution, since it signals failure to the shell. System.exit() is
    a bad idea when the program is run in a container of some kind (applet,
    servlet, etc.), since you don't want to try to shyt down the entire
    container.

    >
     
    Mike Schilling, Dec 16, 2010
    #2
    1. Advertising

  3. mark jason

    Eric Sosman Guest

    On 12/16/2010 11:01 AM, mark jason wrote:
    > hi,
    > I have an application that expects a certain number of command line
    > arguments.I need to check the number of command line args and if
    > sufficient number of arguments are not provided ,would like to print
    > the error message and exit.I have heard that using
    > System.exit(somenumber) is frowned upon.Which is the best practice?
    > regards
    > mark.
    >
    > class MyApp{
    > ....
    > public static void main(String[] args){
    > if (args.length< 4){
    > System.out.println("Usage: java MyApp fname sname
    > citycode statecode");
    > System.exit(1);
    > }
    > String firstname = args[0];
    > String surname = args[1];
    > String cityCode = args[2];
    > String stateCode = args[3];
    > new MyApp().process(firstname, surname, cityCode, stateCode);
    > ...
    > }
    > }


    Since you're detecting the problem in main(), before you've
    done anything like set up database connections or launch forty-two
    worker threads, you could just return. My preference, though, would
    be to throw an exception: It'll make more noise, and be more likely
    to draw someone's attention.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 16, 2010
    #3
  4. mark jason

    Lew Guest

    On Dec 16, 12:01 pm, Eric Sosman <> wrote:
    > On 12/16/2010 11:01 AM, mark jason wrote:
    >
    >
    >
    >
    >
    > > hi,
    > > I have an application that expects a certain number of command line
    > > arguments.I need to check the number of command line args and if
    > > sufficient number of arguments are not provided ,would like to print
    > > the error message and exit.I have heard that using
    > > System.exit(somenumber) is frowned upon.Which is the best practice?
    > > regards
    > > mark.

    >
    > > class MyApp{
    > >      ....
    > >      public static void main(String[] args){
    > >          if (args.length<  4){
    > >              System.out.println("Usage:  java MyApp  fname sname
    > > citycode statecode");
    > >              System.exit(1);
    > >          }
    > >          String firstname = args[0];
    > >          String surname = args[1];
    > >          String cityCode = args[2];
    > >          String stateCode = args[3];
    > >          new MyApp().process(firstname, surname, cityCode, stateCode);
    > >          ...
    > >      }
    > > }

    >
    >      Since you're detecting the problem in main(), before you've
    > done anything like set up database connections or launch forty-two
    > worker threads, you could just return.  My preference, though, would
    > be to throw an exception: It'll make more noise, and be more likely
    > to draw someone's attention.
    >


    I respectfully disagree. Exception messages and accompanying stack
    traces are programmers' artifacts, not users'. A well-designed
    application will translate internal exceptions into appropriate
    external events such as a System.exit(nonZero) or domain-relevant
    error message. Communication with the invoker should be in the domain
    of the invoker, not the internal implementation domain of the service.

    That rule of thumb allows 'System.exit()' for 'main()' applications
    but deprecates it for container-based ones, as mentioned upthread, and
    militates against the release of exceptions unprocessed to the outer
    world.

    --
    Lew
     
    Lew, Dec 16, 2010
    #4
  5. mark jason

    Arne Vajhøj Guest

    On 16-12-2010 11:01, mark jason wrote:
    > I have an application that expects a certain number of command line
    > arguments.I need to check the number of command line args and if
    > sufficient number of arguments are not provided ,would like to print
    > the error message and exit.I have heard that using
    > System.exit(somenumber) is frowned upon.Which is the best practice?


    > public static void main(String[] args){
    > if (args.length< 4){
    > System.out.println("Usage: java MyApp fname sname
    > citycode statecode");
    > System.exit(1);
    > }


    I can not see a problem with that.

    If your app return an exit code, then someone
    running it from a script (.bat, .sh etc.) can
    test on that exit code and handle it appropriately.

    Arne
     
    Arne Vajhøj, Dec 17, 2010
    #5
  6. mark jason

    Nigel Wade Guest

    On 16/12/10 16:01, mark jason wrote:
    > hi,
    > I have an application that expects a certain number of command line
    > arguments.I need to check the number of command line args and if
    > sufficient number of arguments are not provided ,would like to print
    > the error message and exit.I have heard that using
    > System.exit(somenumber) is frowned upon.Which is the best practice?
    > regards
    > mark.
    >
    > class MyApp{
    > ....
    > public static void main(String[] args){
    > if (args.length< 4){
    > System.out.println("Usage: java MyApp fname sname
    > citycode statecode");


    Besides the very useful advice from others, I'd suggest that you report
    the error to System.err rather than System.out, after all that's what
    it's for.

    System.out may be redirected elsewhere, for example, with System.err
    still being displayed on the terminal. If run by cron, System.err may
    generate a different mail report than would System.out. etc.

    --
    Nigel Wade
     
    Nigel Wade, Dec 17, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FuZHk=?=

    Code to Exit Web App and Exit Internet Explorer

    =?Utf-8?B?U2FuZHk=?=, Aug 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    7,949
    =?Utf-8?B?U2FuZHk=?=
    Aug 5, 2005
  2. Joe Smith
    Replies:
    4
    Views:
    66,041
    sandeep1976
    Nov 8, 2006
  3. Replies:
    2
    Views:
    479
    Jeff Epler
    May 31, 2005
  4. Gabriel Genellina

    Re: exit a program gracefully

    Gabriel Genellina, May 4, 2009, in forum: Python
    Replies:
    1
    Views:
    316
    Lawrence D'Oliveiro
    May 5, 2009
  5. Jim King
    Replies:
    7
    Views:
    910
    Jeff Flinn
    Nov 2, 2010
Loading...

Share This Page