Off by one. sum is 7
The try block throws
the ArrayIndexOutOfBoundsException
when the index j
goes beyond the end of the array.
The catch
block catches it, prints out its message, and then execution
continues to the statement after the try/catch
structure.
Now examine this program:
import java.util.* ; public class DivisionPracticeTwo { public static void main ( String[] a ) { Scanner scan = new Scanner( System.in ); int num=0, div=0 ; 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 (ArithmeticException ex ) { System.out.println("You can't divide " + num + " by " + div); } System.out.println("The program is now ending.") } }
Say that the user enters bad data for the second integer
When control reaches the second scan.nextInt()
an exception is thrown.
But there is no catch
block that matches it,
so control leaves the program.
C:\JavaSource>java DivisionPracticeTwo Enter the numerator: 32 Enter the divisor : two Exception in thread "main" java.util.InputMismatchException . . . . .
Will the sentence "The program is now ending" be printed when bad data is entered?