You are going to write some bad code - code that always throws certain exceptions. By explicitly writing code that results in certain exceptions I'm hoping you will better understand how to find such errors with you don't intend them to happen. Next you will write a few methods that call those methods - this is just to deepen the call-stack so you can see what specific information is being reported to you when an exception occurs. Once that is all ready you will call your methods and make sure the really are bad and throw the expected exceptions. Then you will write try catch blocks to catch the various exceptions and notice that by handling them notice that your code does not terminate until you want it too.
You will write a checked and unchecked
exception of your own, add some additional methods to
explicitly throw them and a catch-block to handle them.
Write another method (in the same class as the methods
resulting in exceptions and specified in the prior step) that has an int parameter.
parameter is 1, call one of the three methods that throws. If the
parameter is 2, call the second one, and if the parameter is 3 call the
third one. This is just to add depth to the call stack.
In a separate class (name of your choice), write a main that calls the method you just wrote in the prior step. Write the code so that it prompts the user for and int value (1, 2, or 3) and then call the method you wrote in the prior step. This will allow you to run the program and cause different exceptions to occur. Make sure the program throw each of the three specified exceptions before moving on to the next step.
your main method, add a try block with three catch blocks, one for
exception type that is being thrown by your three methods. Put the code
that prompts the user and calls the specified method in the try
block. In each
catch block print out a message indicating which exception was caught.
Now run your program notice how it behaves differently - it does not
terminate with an exception! This is because you have caught and
"handled" the exception by printing a message. We usually do not handle exceptions
by printing a message but its a start!
Add a finally
block to your try-catch and
have it print out a message. Run your program and notice when the finally block executes. If you make
it to the try block then it always
executes - whether or not an exception occurs while executing the code
in the try block. To verify this add another case to your code in main inside
the try block so that when the user enters a number other than 1, 2, or
3 it prints out a message saying something like "invalid choice" and does not call
one of the methods that throws an exception. When you run your program
and enter an invalid number you should see the message printed by your
Write a checked
exception class and an unchecked
exception class (names
of your choice). In the class with methods that throw the
NullPointerException, ClassCastException, and
ArrayIndexOutOfBoundsExeption add two more methods, one that throws
exception and one that throws your unchecked
exception. Since there is no "bad code" that will result
in the exceptions you wrote simply throw them explicitly. Notice that the
compiler treats your checked
exception differently than your unchecked
exception! Recall from lecture that you will have to uses the throws statement to
declare that your method throws (does not catch) the checked
In the method that accepts an int parameter and calls the different methods that throw the various exceptions add calls the new methods that throw your checked and unchecked exceptions and compile. Again, notice that the compiler treats your checked exception differently! Recall from lecture that every method that calls a method that may throw a checked exception must decide whether it can catch and handle/fix the problem or not. If it can fix the problem it should catch it and fix the problem, otherwise it should not catch it. But, to compile, it will need to tell the compiler that it, too, may throw a checked exception. You will need to use the throws statement to declare that your method throws (does not catch) the checked exception.
Try compiling the class with the main. Notice that it, too, will not compile because it now calls a method that might throw a checked exception. Notice, too, that the compiler does not complain about your unchecked exception - this is the important difference between checked and unchecked exceptions. You could add a throws clause to main to resolve the compiler error. Instead, this time, add a catch block for the checked exception. Notice that this also resolves the compiler error! This is because you have caught the exception and, presumably, fixed it. This means it is longer an exception that could be thrown by main. For the sake of consistency and completeness add a catch block for the unchecked exception too.
Lab courtesy of Kurt Mammen.