# Exception-Based Problem

• December 8th, 2009, 03:48 PM
Hax007
Exception-Based Problem
I was assigned the task of making a program that outputs the number of minutes that have passed since midnight. The program reads the input from the user as h:m:ind, where h is the hour (must be >0 and <=12), m is the minuted (must be >= 0 and <60), and ind is either AM or PM.

Below is ten sample runs of what the program should give the following output.
User input is in bold.

Code :

```Enter h:m:AM or PM > [b]test[/b] Non-numeric Data!   Enter h:m:AM or PM > [b]15:5:AM[/b] Values out of range   Enter h:m:AM or PM > [b]1:15:am[/b] 75   Enter h:m:AM or PM > [b]12:10:am[/b] 10   Enter h:m:AM or PM > [b]1:5:tm[/b] Invalid AM/PM indicator!   Enter h:m:AM or PM > [b]11:50:am[/b] 710   Enter h:m:AM or PM > [b]12:10:pm[/b] 730   Enter h:m:AM or PM > [b]1:25:PM[/b] 805   Enter h:m:AM or PM > [b]11:50:pm[/b] 1430   Enter h:m:AM or PM > [b]12:55:am[/b] 55   Enter h:m:AM or PM > [b]5[/b] Values out of range```

If the entry is valid the program outputs the amount of minutes and ends, otherwise it does one of the following...

"Values out of range!" if h or m is outside its legal range.
"Missing colon!" if a colon is missing.
"Non-numeric data!" if h or m is not an integer.
"Invalid AM/PM indicator" if the indicator is neither AM nor PM (upper or lower)

The hardest part of the assignment is that this program must be executed without any validation and without using any if statements, switch statements or similar construct.

Below is my programs output.
Incorrect outputs are underlined.

Code :

```Enter h:m:AM or PM > [b]test[/b] Non-numeric Data!   Enter h:m:AM or PM > [b]15:5:AM[/b] Values out of range   Enter h:m:AM or PM > [b]1:15:am[/b] 75   Enter h:m:AM or PM > [b]12:10:am[/b] 10   Enter h:m:AM or PM > [b]1:5:tm[/b] Invalid AM/PM indicator!   Enter h:m:AM or PM > [b]11:50:am[/b] 710   Enter h:m:AM or PM > [b]12:10:pm[/b] 730   Enter h:m:AM or PM > [b]1:25:PM[/b] 805   Enter h:m:AM or PM > [b]11:50:pm[/b] 1430   Enter h:m:AM or PM > [b]12:55:am[/b] 55   Enter h:m:AM or PM > [b]5[/b] [u]Missing Colon[/u]```

I believe the source of the problem is RuntimeException. Since it is a superclass, I believe NoSuchElement is being affected, though I could be wrong.

Perhaps there is a better way to throw invalid without using if statements. I thought of using a regex, i.e.

Code :

`String regex = "(1[012]|[1-9]):[0-5][0-9]:(AM|PM|am|pm)";`

However, I couldn't find an exception to throw if it was wrong. Does anyone know if it's possible? If it's not, then it's just a matter of trying to configure the Toolbox.crash to be caught by the RuntimeErrorException rather then the NoSuchElementException (which I still haven't the slightest clue how to do).

Handling exceptions is a very hard/new topic for me so any help would be appreciated, thanks.

Edit: I figured it out..
• December 10th, 2009, 02:53 AM
Json
Re: Exception-Based Problem
Good for you, I'm not really sure how your code works and exceptions is something you need to include in your design from the beginning and decide how you want the flow to be.

For instance you might want to create your own exceptions like InvalidIndicatorException which would be thrown if am/pm was missing or is wrong.

You could on the other hand just decide to use the IllegalArgumentException and throw that with a message of what went wrong or simply just handle your input parsing and output the message to the user straight away, no exceptions thrown or anything.

It all depends on the project in hand and what your needs are for handling errors and exceptions.

// Json