Main Page

Previous Next

Exercises

  1. Write a program that will generate exceptions of type NullPointerException, NegativeArraySizeException, and IndexOutOfBoundsException. Record the catching of each exception by displaying the message stored in the exception object, and the stack trace record.

  2. Add an exception class to the last example that will differentiate between the index-out-of-bounds error possibilities, rethrow an appropriate object of this exception class in divide(), and handle the exception in main().

  3. Write a program that calls a method which throws an exception of type ArithmeticException at a random iteration in a for loop. Catch the exception in the method, and pass the iteration count when the exception occurred to the calling method, by using an object of an exception class you define.

  4. Add a finally block to the method in the previous example to output the iteration count when the method exits.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor