Exception Handling, Exercises

A

Create a class with a main() method that throws an exception of type Exception (from inside a try block). Catch the exception with a catch clause. Next, provide a string argument when the exception is thrown print the string message in the catch clause. Third, add a finally clause that print a message to the screen. Explain the control flow of the program.

B

Define an object reference and assign the value null to it. Try to call a method on the null object. Next, wrap the method call in a try-catch clause.

C

Create three new types of exceptions and write a method that throws all three. In a main() method call the method but use only a single catch clause to catch all three types of exceptions.

D

Create a program example that shows that a subclass cannot catch an exception thrown by a superclass's constructor.

Extra Assignments

E1

Extended Assignment B with a finally clause. Show that finally clause is executed even when exceptions are thrown.

E2

Write a program that creates and catches an ArrayIndexOutOfBoundsException.