Com S 362 --- Object-Oriented Analysis and Design EXERCISE: EXCEPTION HANDLING (File $Date: 2003/08/29 17:03:18 $) The purpose of this exercise is for you to learn about exceptions and exception handling in Java. Exception handling is also a feature in C++, but is not frequently used in our introductory classes. As with all exercises, this is to be done individually, not in teams. And it is due the day this topic is planned to be discussed in class, unless specified otherwise (see the syllabus at: http://www.cs.iastate.edu/~cs362/syllabus.shtml). As with all exercises, you have two choices for doing the work. You can either: - complete it as specified or - write down questions or problems that you had in trying to complete it. If you write down questions or problems you have, these should be detailed enough so that we can tell that you have read the materials and thought about them. (Don't just write: "I couldn't get it to work; say what you tried and what you didn't understand.) During the class where this exercise is discussed, you should try to get help with these by explaining what you did and what your problems or confusions are. Don't be shy; there will be other people with the same problem, and everyone can learn by discussing these issues. 1. [Why exceptions?] Read chapter 8 of Ken Arnold, James Gosling, and David Holmes's book The Java Programming Language Third Edition (Addison-Wesley, Reading, Mass., 2000). (You may want to refer back to chapters 1-3 of that book if you don't understand some concepts. In particular, section 3.1 explains the notion of subclassing, or inheritance, in Java.) Answer the following questions. a. What are the reasons someone would design a method that throws exceptions? b. What are the alternatives to exception handling and when might exceptions be better? 2. [Throwing exceptions] Read chapter 8 of Ken Arnold, James Gosling, and David Holmes's book The Java Programming Language Third Edition (Addison-Wesley, Reading, Mass., 2000). a. What does the phrase "throw an exception" mean in Java? b. What happens when a piece of code throws an exception? c. Modify the following piece of code so that it throws an IllegalArgumentException when its argument, y, is negative. Make sure your answer compiles. (Note that IllegalArgumentException is built-in to Java, and lives in java.lang.) Add a main method to this class and write a driver to test that it works. Your driver should pass isqrt a negative argument for y and catch the IllegalArgumentException exception, and print a message if it does not get the exception. package exceptions; /** A class of integer mathematical operations. */ public class IntMathOps { /** Return the integer square root of the argument. * @param y the number to take the square root of. */ public static int isqrt(int y) { return (int) Math.sqrt(y); } } Hand in a printout of your code and the output from a run of the program. d. What does the following code do? package exceptions; /** A test. */ public class TestExceptions1 { /** A mystery method. */ public void test1() { Object x = null; if (x.equals(null)) { System.out.println("hmmm"); } else { System.out.println("oh"); } System.out.println("got to end of test1"); } /** Run the test. */ public static void main(String[] args) { new TestExceptions1().test1(); } } e. Can you explain what happened in TestExceptions1? f. What does the following code do? package exceptions; /** A test. */ public class TestExceptions2 { String s; /** A mystery method. */ public void test2() { if (s.equals(null)) { System.out.println("hmmm"); } else { System.out.println("oh"); } System.out.println("got to end of test2"); } /** Run the test. */ public static void main(String[] args) { new TestExceptions2().test2(); } } As always if you cannot do this or get stuck, write down the specific problems or questions you encountered instead. (However, even if you cannot do this or get stuck, you must continue on with the rest of the questions in this exercise. You must can write down questions for them too, but you have to look at everything). 3. [Declaring exceptions] Read chapter 8 of Ken Arnold, James Gosling, and David Holmes's book The Java Programming Language Third Edition (Addison-Wesley, Reading, Mass., 2000). a. In Java, what kinds of exceptions have to be declared by a method? (Describe them.) b. In Java, what kinds of exceptions do not have to be declared by a method? (Describe them.) c. Why would Java want to have both kinds of exceptions? d. Suppose you are declaring a new kind of exception, how do declare whether it is a checked exception? e. When would you declare a new exception to be a checked exception? 4. [Handling exceptions] Read chapter 8 of Ken Arnold, James Gosling, and David Holmes's book The Java Programming Language Third Edition (Addison-Wesley, Reading, Mass., 2000). Note: some of the questions below are adapted from The Java Tutorial Third Edition: A Short Course on the Basics, by Mary Campione, Kathy Walrath and Alison Huml (Addison Wesley, 2000), which is available on-line at: http://java.sun.com/docs/books/tutorial/books/3e/toc.html. a. In what kinds of situations would you want to use a "finally" clause in a try-statement? b. Give an example of at least one of these situations would you want to use a "finally" clause in a try-statement? c. What kinds of exception are caught by the following handler? try { ... catch (Exception ex) { ... } d. Is there anything wrong with using the exception handler given in part (c)? WHAT TO HAND IN You should have at the beginning of class, written answers to the above questions (or written out questions and problems you encountered for each part). Make sure your name is on these. Attach the printouts, if any, requested above. ADDITIONAL READINGS If you are new to Java you might also want to read the interesting (to you) parts of the "new to Java" section on-line at http://developer.java.sun.com/developer/onlineTraining/new2java/ (If you read that link, I would advise starting with the "step by step programming" step, which is step 3.)