PDA

View Full Version : Stopping Java Application depending on user input


BWiz
09-25-2008, 01:03 AM
I'm coding a little program that calculates the difference between an arrival time and departure time. Below is a little snippet of the app that calculates the difference:


public int DifferenceInHours(int Departure, int Arrival) {
int difference = 0;
if ( Departure >= Arrival ) {
System.out.println("The times you entered seemed to be incorrect.");
// I want to stop here...
}
difference = Arrival - Departure;
return difference;
}
Now say, if the departure time is greater than the arrival time, how would I stop the application completely from processing any further?

Thanks,
BWiz.

shyam
09-25-2008, 08:29 PM
throw an exception...or if you really want to terminate use System.exit()

BWiz
09-26-2008, 12:37 AM
What exception would I throw?

shyam
09-26-2008, 06:26 PM
What exception would I throw?

anything that is logical in that particular situation...IllegalArgumentException comes close...

ess
09-26-2008, 10:56 PM
You can always create class that inherits from the exception class or IllegalArgumentException class for that matter...which explicitly describes the business logics of this requirement where dates are not entered correctly by the end user.

Doing so guarantees that you are able to track errors, log them appropriately, and inform users with correct error messages whilst using your custom application.

Cheers
~E