The catch{}
blocks are in a correct order,
because ArithmeticException
is not
an ancestor nor a descendant of InputMismatchException
.
Reversing the order of the two blocks would also work.
In the example program,
the try{}
block
might throw
a NumberFormatException
,
or
an ArithmeticException
.
public static void main ( String[] a ) . . . . try { System.out.print("Enter the numerator: "); num = scan.nextInt(); System.out.print("Enter the divisor : "); div = scan.nextInt(); System.out.println( num + " / " + div + " is " + (num/div) + " rem " + (num%div) ); } catch (InputMismatchException ex ) { . . . } catch (ArithmeticException ex ) { . . . } }
A NumberFormatException
might occur in either call to
nextInt()
.
The first catch{}
block is for this type of exception.
An ArithmeticException
might occur if the user enters
data that can't be used in an integer division.
The second a catch{}
block is for this type of exception.
What type of exception is thrown if the user enters a 0 for the divisor?