
Assignment No :02
Due date: 20th Oct, 2011
1.My first try/catch
Create a class with a main that throws an object of class Exception inside a try
block. Give the constructor for Exception a String argument. Catch the exception
inside a catch clause and print the String argument.
2. My first own exception
Create your own exception class using inheritance. Write a constructor for this
class that takes a String argument and stores it inside the object. Write a method
that prints out the stored String. Create a try-catch clause to exercise this new
exception.
3. Exception specification
Write a class with a method that throws an exception of the type created in
Exercise 2. Try compiling it without an exception specification to see what the
compiler says. Add the appropriate exception specification. Try out your class
and its exception inside a try-catch clause.
4. Null pointer exception
Define an object and initialise it to null. Try to call a method through this refer-
ence. Now wrap the code in a try-catch clause to catch the exception.
5. Beyond the array's bound
Write a program to generate and catch an ArrayIndexOutOfBoundsException.
6. Chained handlers
Create a class with two methods, f( ) and g( ). In g( ), throw an exception of a
new type that you define. In f( ), call g( ), catch its exception and, in the catch
clause, throw a different exception (of a second type that you define). Test your
code in main method.
7. Hierarchical exceptions
Create a three-level hierarchy of exceptions. Now create a base-class A with a
method that throws an exception at the base of your hierarchy. Inherit B from A
and override the method so it throws an exception at level two of your hierarchy.
Repeat by inheriting class C from B. In main, create a C and upcast it to A,
then call the method. Demonstrate that a derived-class constructor cannot catch
exceptions thrown by its base-class constructor.
8.Finally clause I