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.
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 ) { . . . } }
In the example program,
the try
block
might throw
a InputMismatchException
,
or
an ArithmeticException
.
A InputMismatchException
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?