// Clear the output field
result.setText("");
// The try block, which will convert the Strings in the text fields into
// double's. If anything has been inputted incorrectly, then an exception
// will be raised.
try {
double number1=Double.parseDouble(amount.getText());
double number2=Double.parseDouble(rate.getText());
double res = number1*number2;
if(number1 == 0.0d || number2 == 0.0d) throw new ArithmeticException("Divide by zero");
result.setText(String.valueOf(res));
}
catch (NumberFormatException n) {
JOptionPane.showMessageDialog(this,
"You must enter two numbers",
"Invalid number format",JOptionPane.ERROR_MESSAGE);
}
catch (ArithmeticException d) {
System.out.println("ArithmeticException "+d);
JOptionPane.showMessageDialog(this,
"You must enter two numbers",
"One of the two numbers is zero",JOptionPane.ERROR_MESSAGE);
}