how to exit gracefully

M

mark jason

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);
...
}
}
 
M

Mike Schilling

mark jason said:
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.
 
E

Eric Sosman

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.
 
L

Lew

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.
 
A

Arne Vajhøj

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
 
N

Nigel Wade

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top