Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to exit gracefully

Reply
Thread Tools

how to exit gracefully

 
 
mark jason
Guest
Posts: n/a
 
      12-16-2010
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);
...
}
}

 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      12-16-2010
"mark jason" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

>

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-16-2010
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      12-16-2010
On Dec 16, 12:01*pm, Eric Sosman <(E-Mail Removed)> 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
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      12-17-2010
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
 
Reply With Quote
 
Nigel Wade
Guest
Posts: n/a
 
      12-17-2010
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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Force unwinding stack, exit gracefully. Jim King C++ 7 11-02-2010 11:53 AM
Re: exit a program gracefully Gabriel Genellina Python 1 05-05-2009 11:28 AM
Exit code of a batch (using exit /B) Joe Smith Java 4 11-08-2006 12:25 PM
Code to Exit Web App and Exit Internet Explorer =?Utf-8?B?U2FuZHk=?= ASP .Net 7 08-05-2005 01:55 AM



Advertisments