6.8 Problems

 < Day Day Up > 



6.8 Problems

  1. Because an ArithmeticException is unchecked, it does not have to be caught in a try block or explicitly thrown. The program below catches the ArithmeticException, a zero divide, in the program using an ancestor of the ArithmeticException, class Exception. However, when the ArithmeticException is re-thrown, it must now be caught. Explain the behavior of this program.

       public class Problem61 {     public static void method1() {       try {         int i = 0, j = 7, k;         k = j/i;       } catch(Exception e) {         if (e instanceof ArithmeticException) {           System.out.println("Arithmetic Exception raised");           throw e;         }       }     }   } 

  2. Can a constructor throw an exception? If the constructor can throw an exception, what must be true of the default constructors of any class that extends it?

  3. This program continues the thought in Problem 2. In this case, thePerson object can throw an AgeNotValidException. Using the Java extends clause, extend this class into an Employee class. What happens when you use the default constructor in your Employee class?

       class AgeNotValidException extends Exception {   }   class Person {     int age;     public Person(int age) {       if (age < 0 || age > 120)        throw new AgeNotValidException();       this.age = age;     }   } 

  4. In the following program, an interface is defined that specifies that an exception can be thrown from this method, but the method as implemented does not throw the method or include it in its signature. Is this valid or invalid in Java? Why was it defined this way in Java?

       interface HasAge {     public void setAge(int age) throws AgeNotValidException;   }   class Person implements HasAge {     int age;     public void setAge(int age) {       this.age = age     }   } 

  5. In the following program an interface is defined. The class that implements the interface now tries to throw an exception that is not defined in the interface. Is this valid or invalid in Java? Why do you think Java implemented this behavior?

       interface HasAge {    public void setAge(int age);   }   class Person implements HasAge {     int age;     public void setAge(int age) throws AgeNotValidException {       if (age < 0 || age > 120)         throw new AgeNotValidException();       this.age = age     }   } 

  6. Implement the program from Problem 5.4 with exceptions. The exceptions needed for this problem are defined here:

    • TableException is the parent of all other exceptions.

    • TableFullException should be thrown from the add method when an insert is attempted on a table that is full.

    • ObjectNotFoundException should be thrown when a delete is attempted on an object that is not in the current table.

    • UnmatchedTypesException should be thrown from the eq and gt methods of the class that implements the Sortable interface. It should be thrown if the type of object passed as a parameter to this method is not compatible with the current object. While this object is generated from the eq and gt methods, it should be propagated out of the add and delete methods.

    Note 

    You must also redefine the Sortable interface as follows:

       interface Sortable {     public boolean gt(Sortable s) throws       UnmatchedTypesException;     public boolean eq(Sortable s) throws       UnmatchedTypesException;   } 

  7. In Problem 6, if an UnmatchedType exception is not needed and if the types do not match, an attempt could be made to cast the object and a ClassCastException allowed to be raised if the objects are not of compatible types. What is the advantage of defining an exception specifically for this case in our program? (Hint: Consider what exceptions each of these extends.)

  8. The text mentioned that exception handling was expensive and you should not use an exception when a simple test would be sufficient. You should prove this. To do so, notice that the java.util.Stack object has an empty method and throws an EmptyStackException if a pop is attempted when no items are on the stack. This means that a pop operation on a stack can be handled in two ways:

    1.        if (! stack.empty())         stack.pop(); 

    2.        try {         stack.pop();       } catch(EmptyStackException) {       } 

    Use the System.currentTimeMillis method to time both of these ways of popping a stack by pushing and popping 1,000,000 objects. Which method takes longer? How much longer? Explain why one takes longer than the other.

  9. What is the result of running the program shown in Exhibit 18?

    Exhibit 18: Program to Problem 6.9

    start example

     class Except1 extends Exception { } public class TE {   public static void method2() throws Except1 {     throw new Except1();   }   public static void method1() {     try {       method2();       System.out.println("After method 2");     } catch(Except1 e) {       System.out.println("Caught Except1 in method 1");     } catch(Exception e) {       System.out.println("Caught Exception in method 1");     } finally {       System.out.println("Finally in method 1");     }       System.out.println("Leaving method1");     }   public static void main(String args[]) {     try {       method1();     } catch(Exception e) {       System.out.println("Caught Exception in main");     } finally {       System.out.println("Finally in main");     }     System.out.println("Leaving main");   } } 

    end example



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net