I am writing an application in Java which use MVC pattern design. I have 4 major classes: Main, Controller, Model, View. Main initialize the other three and run launchMenu() in a infinite loop, a method included in Controller. This launchMenu from Controller runs a launchMenu method of another class: MenuHandler, which set up the Actions (using my own Command design pattern) and assign them to each option from my enum UserOperationsMenu.
I have several doubts on how to handle UserOperationsMenu.EXIT.

My question is about what would be better and more logic:

  1. Using an exception I catch up in the body of Main's loop and throw it in my ExitAction class assigned to UserOperationsMenu.EXIT?
  2. Creating an object Delegate in Controller which I pass to MenuHandler and ExitAction so that I can command from ExitAction to exit the program?

I've thought about using a boolean variable in my while in Main, and returning false when I want to finish the program. But I don't like the idea, though.

NOTE: I don't want to use System.exit(0) directly because first I have to do several tasks outside my ExitAction.

  • 1
    Option 2 seems straightforward. I'm not a big fan of using exceptions for flow control. Basically, handle the menu entry like you would handle any other menu entry. – Robert Harvey Apr 23 '14 at 17:34
  • Thanks Robert. It's true using an exception is not the cleanest way to solve my issue. It was only a better idea than returning false to exit the application. – Jorge Vicente Cantero Apr 24 '14 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.