What is an appropriate way to programmatically exit an application?

Posted by denchr on Stack Overflow See other posts from Stack Overflow or by denchr
Published on 2009-11-15T01:18:13Z Indexed on 2010/03/27 11:53 UTC
Read the original article Hit count: 131

I am evaluating user inputs as commands for my application. If the user presses Q, or q, and then hits enter, the application quits and execution terminates.

Is there a proper context, or best practices on how to do that? I do not have any resources to release, or anything like that. Should I just use System.exit(0);? Is there a recommended way to do that?

As my first approach I do something like this:

while (true){
    try{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        //Other logic goes here...
        if (br.readLine().equalsIgnoreCase("Q")){
            System.exit(0);
        }
    }
    catch (IOException ioe) {
        System.out.println("IO error trying to read your selection");
    }
}

© Stack Overflow or respective owner

Related posts about standard-output

Related posts about user-input