Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 11 February 11, 2003. © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not.

Similar presentations


Presentation on theme: "CSE 11 February 11, 2003. © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not."— Presentation transcript:

1 CSE 11 February 11, 2003

2 © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any other purpose without the written permission of Walter Savitch. wsavitch@ucsd.edu

3 No Midterm Grade Appeals Accepted After February 18

4 Exception Handling Way to separate handling of “exceptional cases” from the “ordinary” cases. Throwing an exception is a trigger event Handling the exception is the action produced in reaction to this trigger event.

5 try { … if (Something) throw new Exception(“Bla Bla"); … } catch(Exception e) { Handle Exception Action }

6 How Commonly Used: try { … someMethod(…);//Might throw exception … } catch(Exception e) { Handle Exception Action }

7 How Commonly Used: public void someMethod(…) throws Exception { if (something) handle things else//handling depends on context throw new Exception(“Bla Bla”); }

8 How Commonly Used: public int readInt()throws Exception { String s = SavitchIn.readLine(); if (s is well formed) return Integer.parseInt(in); else//handling depends on context throw new Exception(“Syntax error”); }

9 Throws Clause public int readInt()throws Exception Warns user of method that method might throw an exception Enforced by compiler

10 throws Clause in Derived Classes Cannot add any exception classes to the throws clause of an overridden method definition. As always the rules for invoking the overridden method must not be added to Can delete exception classes from the throws clause

11 Exceptions that Do Not Need to Be Caught

12 Defining Exception Classes Must be a derived class of an existing exception class, typically a derived class of Exception. Typically all it has and all it needs for methods are constructors and inherited methods.

13 public class DivideByZeroException extends Exception { public DivideByZeroException( ) { super("Dividing by Zero!"); } public DivideByZeroException(String message) { super(message); }

14 getMessage() Method Inherited Also inherited a private instance variable of type String. getMessage() is an accessor method that returns the string in this private instance variable.

15 Multiple catch blocks When exception is thrown, try catch blocks in order. First one that fits the type of the thrown exception wins.

16 Back to Inheritance

17 Abstract Class Has methods with no definition. These are labeled “abstract”. Class is labeled “abstract” Cannot have objects (directly) of an abstract class type. Used only as a base case

18 public abstract class Figure { private int offset; public abstract void drawHere( ); public void drawAt(int lineNumber) { int count; for(count=0; count<lineNumber; count++) System.out.println( ); drawHere( ); }... }

19 Interface A collection of method headings (like a very abstract class). Not a class, but is a type. A class can have only one base class, but A class can implement any number of interfaces.

20 public interface Writable { public String toString(); public void writeOutput(); }

21 public class WritableUndergraduate extends Student implements Writable { private int level; public String toString( ) { a full definition } public void writeOutput( ) {a full definition } Other Stuff }

22 public interface Comparable { public int compareTo(Object o); } Actually a predefined interface with some assumptions oh how compareTo behaves

23 public static void sort(Comparable[] a) { some sorting code }

24 Dynamic Binding Other terms for the same thing: Late Binding, Polymorphism Every object knows which version of a method definition that it should use. Automatic in Java

25 public void println(Object o) { System.out.println(o.toString( )); } Without dynamic binding this would always use the definition of toString defined in Object.

26 public void println(Object o) { System.out.println(o.toString( )); } With dynamic binding this use the definition of toString defined when the object was created. WriteableUndergraduate s = new W… System.out.println(s);


Download ppt "CSE 11 February 11, 2003. © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not."

Similar presentations


Ads by Google