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.

Slides:



Advertisements
Similar presentations
Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
1 Week 11 l Basic Exception Handling »the mechanics of exceptions l Defining and Using Exceptions »some "simple" cases l Reality Check »guidelines for.
Exception Handling Chapter 8. Outline Basic Exception Handling Defining Exception Classes Using Exception Classes.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Exception Handling By: Thomas Fasciano. What is an Exception?  An error condition that occurs during the execution of a Java Program.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Inheritance Recitation - 02/22/2008 CS 180 Department of Computer Science, Purdue University.
CSE 11 February 6, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Important Annoucement 1  I messed up something in the last class  if a subclass overrides a method that throws an exception then it must either 1. throw.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Written by: Dr. JJ Shepherd
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Interfaces and Inheritance
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Announcements/Reminders
Inheritance, Polymorphism, and Interfaces
Exceptions & exception handling
Inheritance Chapter 7 Chapter 7.
Inheritance Basics Programming with Inheritance
Exceptions & exception handling
Inheritance, Polymorphism, and Interfaces. Oh My
CMSC 202 Interfaces.
Inheritance, Polymorphism, and Interfaces. Oh My
Exception Handling Chapter 9 Edited by JJ.
Computer Programming with JAVA
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CMSC 202 Interfaces.
CMSC 202 Exceptions.
CMSC 202 Interfaces.
Presentation transcript:

CSE 11 February 11, 2003

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

No Midterm Grade Appeals Accepted After February 18

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.

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

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

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

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”); }

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

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

Exceptions that Do Not Need to Be Caught

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.

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

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.

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

Back to Inheritance

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

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( ); }... }

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.

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

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

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

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

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

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

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);