29 lines
1.0 KiB
Java
29 lines
1.0 KiB
Java
import javax.swing.*;
|
|
import java.util.InputMismatchException;
|
|
|
|
public class ExceptionsDemo {
|
|
public static void main(String[] args) {
|
|
int numerator = 0;
|
|
int denominator = 0;
|
|
int result;
|
|
|
|
String inputString;
|
|
|
|
try {
|
|
inputString = JOptionPane.showInputDialog(null, "Enter a number to be divided");
|
|
numerator = Integer.parseInt(inputString);
|
|
inputString = JOptionPane.showInputDialog(null, "Enter a number to divide into the first number");
|
|
denominator = Integer.parseInt(inputString);
|
|
result = numerator / denominator;
|
|
|
|
JOptionPane.showMessageDialog(null, numerator + " / " + denominator + "\nResult is " + result);
|
|
} catch (ArithmeticException exception) {
|
|
JOptionPane.showMessageDialog(null, exception.getMessage());
|
|
result = 0;
|
|
} catch (NumberFormatException exception) {
|
|
JOptionPane.showMessageDialog(null, "Please enter only Integers!");
|
|
result = 0;
|
|
}
|
|
}
|
|
}
|