CSC152 2004F, Class 19: Exceptions Admin: * About the need for convo. * Thursday's convo meets in Harris * Subject: Images of the body, particularly in tech. age * Warning: The last time I went to a convo on "images of the body inthe technological age" in Harris, at 12:15, the speake was still going strong * Advance warning about week 7: Sam absent on Wednesday and Friday. No class, no assignments. * Questions on the exam. * Homework due and assigned Overview: * Exceptions, concluded * Review * Throwing exceptions * Creating new exceptions * About the homework * Background * Goals of course * The project /Exam Stuff/ * Sam, can you show me a sample Comparator? * See the rebelsky.newvec package, particularly CompareVectorsByRadius * CompareStudentById and CompareStudentByName should implement Comparator. They should not be subclasses of Student. * Evil trick in Java. Every class implicitly subclasses java.lang.Object (aka Object). java.lang.Object provides default implementations of all the key methods. * toString: Default class@address * equals: Default: A lot like eq? in Scheme * hashCode: Default: address * clone: Default: I don't know * others we haven't learned yet * Must we use exceptions on the exam? * No. * Can we use exceptions on the exam? If we do, will we earn extra credit? * Yes, you may use them. * Particularly nice uses may earn you extra credit. * Detour: + Homework grading: "Turned in and works correctly (and well documented)": Check 3 "Turned in and doesn't work": Minus 2 "Not turned in": Zero "Turned in and suprises me with its beauty": Plus 4 Mostly checks: B Mostly minuses: C Mostly zeros: 0 Mostly plusses: A + Exam grading: Start with 100 Each error removes points Each "wow!" adds points Note: The 80 guaranteed is *after* the ec has been applied * Documentation on the exam is *optional* /Exceptions, concluded/ * Review * A mechanism for methods to say "I'm sorry, I can't do what you requested" * To warn in advance that a method may fail public TYPE NAME(PARAMS) throws TYPEOFEXCEPTION,TYPEOFEXCEPTION * Lazy solution public TYPE NAME(PARAMS) throws EXCEPTION * Methods that call exceptional methods have two choices * Deal with the problem try { EXCEPTIONALMETHOD(PARAMS); } catch (Exception e) { DEALWITHTHEPROBLEM } * Deal with different kinds of problems differently try { EXCEPTIONALMETHOD(PARAMS); } catch (ExceptionOne e1) { DEALWITHONEKINDOFPROBLEM } catch (ExceptionTwo e2) { DEALWITHANOTHERKINDOFPROBLEM } * Pass the problem on to their caller Declare "throws Exception" in the method header * Throwing exceptions throw new TYPEOFEXCEPTION(PARAMS) public int compareTo(Object other) throws WrongTypeException,BadDataException { if (!(other instanceof Vec2D)) { throw new WrongTypeException("I can only compare myself to vectors"); } Vec2D ovec = (Vec2D) other; if (ovec.radius < 0) { throw new BadDataException("Vectors should not have negative radii"); } ... // code as before } // compareTo(Object) * Safest strategy: Always use Exception, not specific types of exception * Easy * Loses expressivity * Creating new exceptions public class MyException extends Exception { public MyException() { super(); } public MyException(String note) { super(note); } } // class MyException * About the homework /The Project/ * Three aspects to CSC152 * How to use the equipment: Java [Mostly done] * How to design experiments: Programming [Throughout the semester] * Basic knowledge of the scientific domain: Algorithms, ADTs, and Data Structures [Starts soon] * TO learn to program well, you need more than "toy" problems * Real errors/difficultlies more likely to occur * Communication is central to programming * Group work is central to programming * Many students learn better by grounding their theory in practice * Sam's strategy for "real" programming: BIG projects that everyone contributes to * We have to choose a project * We have to choose groups * Tomorrow we will do both