views:

77

answers:

5

In my calculation app, which takes a few inputs and uses them in a fixed formula to produce multiple outputs, the application crashes if I try to run it without putting a value into every input.

I know how to catch exceptions, but I don't know the name of the exception that would be thrown by this kind of error. Can anyone help me out on this?

+1  A: 

Look at the stack trace for the class name of the exception.

If I had to guess, I would say it's probably a NumberFormatException

tjlevine
+1  A: 

Also if you are using an IDE, you can attach source code of SDK and directly lookup the source to see what all exception it throws and where. That way you will be able to handle exception at correct places.

Ravi Gupta
+1  A: 

I agree it sound like a NumberFormatException happening when your Double.parseDouble(s) fails on an empty or null string.

If you input control allow alpha-numeric input you can (and should) catch this, but for user-friendliness, you should make sure the string isn't empty first. You can then provide two useful error message:

  • "Please enter a value" (if the string is null or blank)
  • "Please enter a number" (if you caught the parser exception)

You might also consider using or building an input control that only allows for correctly formatted numeric input, including never passing back a blank. If you have this sort of control, you should not catch the parse exception since none should happen, and if does, this means there is a BIG problem and you really want to know about it by failing loudly.

If you are using a numeric-only control, then it's probably just that you are getting back an empty string.

rickmode
+1  A: 

If you can avoid triggering an Exception, try rewriting your code to check whether those input fields are empty, and if applicable, if those input are numeric. Catching an Exception sometimes is a big performance hit.

Devmonster
A: 

Thanks for the tips guys. It was indeed a NumberFormatException. I wanted the values to default to zero if unentered so before parsing them in an equations i called them individually and set up catches for each one.

Thanks again for all the help!

Sean