Docsity

Java Exceptions Assignment Solutions, Exercises of Advanced Computer Programming

Toggle sidebar
bg1
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
docsity.com docsity.com
pf2

Partial preview of the text

Download Java Exceptions Assignment Solutions and more Exercises Advanced Computer Programming in PDF only on Docsity!

Assignment No : 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.

  1. 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.
  2. 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.
  3. 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.
  4. Beyond the array's bound Write a program to generate and catch an ArrayIndexOutOfBoundsException.
  5. 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.
  6. 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

docsity.comdocsity.com

Add a finally clause to the program created in Exercise 1 and print a message to prove you were there.

  1. Finally clause II Create three new types of exceptions. Write a class with a method that throws all three. In main, call the method but only use a single catch clause that will catch all three types of exceptions. Modify the program by adding a finally clause. Verify that your finally clause is executed, even if a NullPointerException is thrown.
  2. Largest integer Write a class with an integer addition method. This method has to return the largest positive integer represent able on the machine in case the method over ows. Test your code in main method.
  3. Swap attributes Create a class with two Integer attributes. Create a method to swap the attributes' values. Throw an exception of a new type that you define at different places within the method. The catch block has restore the attributes' values as held before calling the method. Test your code in main method.
  4. Swap arrays Adapt the class of the previous exercise to swap two arrays of dimension four. Test your code in main method.
  5. Sorting I Create a class with a method that sorts the elements of an array of dimension 5. Throw an exception of a new type that you define at different places within the method. The catch block has to sort the same array, but using a different sorting algorithm.
  6. Sorting II Extend the method of the previous exercise such that if an exception is thrown within the catch block, then the method has to print out only the sorted part of the array.

docsity.comdocsity.com