Exception Handling Exercises

#1
Modify the code in the example so that instead of calling the
quotient method (line 70), the division is performed in the actionPerformed method using the line
   
        
result = (double) number1 / number2;
You should catch the DividesByZeroException at that point. That will force you to experiment with the throws statement.


#2
Modify either the example or your results from #1 above so that instead of catching a
DivideByZeroException (line 79) it simply catches and throws the built-in ArithmeticException. That will teach you how to use the exceptions that are provided....


#3
Modify the code in the example by creating a new exception that detects the use of an object before it is instantiated. Call your new exception
UninstantiatedObjectException and
extend NullPointerException.

An example of the exception that you are trying to catch would be trying to add a component to an applet before instantiating it. For example, if line 41 was omitted from the program, line 42 would generate a NullPointerException. Try commenting out line 41, and enclosing lines 36-56 in a try-catch block. The first line in the try block should be a test to determine if the component (input1) is equal to null, i.e., it has been declared but not instantiated. If it equals null then the program should throw an UninstantiatedObjectException. You can also test input2 and output.  Your catch section should print an appropriate message using a System.out.println line to print your exception message.

 


To make the exercise easier for you, you can download the code here.