I'm learning java and I'm trying to make a very simple application that does conversion of currency. You enter a rate, a direction (e.g : from euro to dollars or reverse) and an amount. the numbers valid non negative numbers.
So far I managed to make it so that the number can't be negative; now I need to throw an error if it's not a number. I have following code:
public void setKoers(double koers) throws NegativeValueException, NumberFormatException{ if (koers > 0 ) { this.koers=koers; } else { throw new NegativeValueException("negative number"); } } and my main looks like
try { cal.setKoers( Double.parseDouble(args[0])); } catch(NegativeValueException e) { System.out.println(e.getMessage()); } catch (NumberFormatException e) { System.out.println( e.getMessage()); } So how can I check if koers is a number or not. I know I could put try and catch the error in my code, but I think this would go against the logic of where and how to deal with errors: in my main function I should catch any NumberFormatException