I don't know of any numbers that can add up to NaN or Infinity, so I assume that this would be like if the user enters 'cat' or something like that right?
First, cast you're numbers and catch the exception tossed, then evaluate. I'd probably just throw on a check for it, but I have a feeling that Java may do that for you when you're trying to perform you're calculations. Its been a couple of years since I've done work in java (gotta get back into it though...), so I may be mistaken on that.
Another option is to let the method throw the exception, and catch it with you're main program. The advantage of this is that it forces the developer of the interface to gracefully handle the exceptions. Just add a throws Exception (or whatever exception type you want) as a part of you're method signature.
double d1, d2, result;
d1 = Double.valueOf(txtNumber1);
d2 = Double.valueOf(txtNumber2);
result = d1 + d2;
throw new Exception('NaN result!');
throw new Exception('Result is Infinite');
catch (Exception ex)
// Do something, one of these didn't parse.
Ok, hope that helps!