Exception Handling, Exercises


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.


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.


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.


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

Extra Assignments


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


Write a program that creates and catches an ArrayIndexOutOfBoundsException.