Shaun Conlon CS 211 Section 6 4/3/2003 Notes Topics for this week: -A5 wrapup -Trees with parent pointers -Exceptions 1) A5 wrapup See the course website. 2) Trees with parent pointers This topic isn't really that important in that you don't need to know trees with parent pointers for the exam, but this is to give you an example of how to develop the code for a particular data structure and its associated methods. This was an in class exercise, so I won't post the code here. We developed the following classes and methods: class Node methods: add(Object o), delete(Node n), getHeight(), getDepth(Node) For more fun, see Kiri's section 10 notes from spring 2002 (http://www.cs.cornell.edu/Courses/cs211/2002sp/). 3) Exceptions try/catch: When you call methods that may throw exceptions, it is usually a good idea to catch those exceptions so they don't crash your program. You do this with a try/catch statement: try { // code that may throw (an) exception(s) } catch { } { // code in case exception occurs } catch { } { // code in case exception occurs } /... You may catch multiple exceptions in one try/catch clause. Always remember to move from specific to general when catching exceptions. You can use printStackTrace if you want to output the source of the exception. Exceptions have a heiarchy (see the Java API). The top of the heiarchy has class Exception, which is the superclass of all other Exceptions. If you want to catch any Exception in your code, then you should use Exception as the in the code above. throw, throws: -If you write a method that may throw an exception that you want users of the method to catch, then you should declare this with a throws clause in the method signature. For example: public void myMethod() throws Exception { /... } -To throw an exception, you use the throw keyword: // code throw new Exception(); Writing your own exception class: To write your own exception class, you need to extend one of the other exception classes: public class MyException extends Exception { public MyException(String s) { super(s); } public MyException() { super(); } } Email me (sc235) with any questions.