© 2004 Pearson Addison-Wesley. All rights reserved November 30, 2007 Exceptions ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:

Slides:



Advertisements
Similar presentations
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Advertisements

CS102--Object Oriented Programming
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Chapter 12 By Tony Gaddis Modified by Elizabeth Adams Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
File I/O and Exceptions File I/O Exceptions Throwing Exceptions Try statement and catch / finally clauses Checked and unchecked exceptions Throws clause.
Java Programming Transparency No. 1-1 Java Exception Handling Cheng-Chia Chen.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Input/Ouput and Exception Handling. 2 Exceptions  An exception is an object that describes an unusual or erroneous situation  Exceptions are thrown.
Java Software Solutions Foundations of Program Design Sixth Edition
Preventing and Correcting Errors
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
1 Advanced Flow of Control - Introduction - zTwo additional mechanisms for controlling process execution are exceptions and threads zChapter 14 focuses.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
CIS 270—Application Development II Chapter 13—Exception Handling.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
10-1 Exceptions An exception is an object that describes an unusual or erroneous situation Exceptions are thrown by a program, and may be caught and handled.
Chapter 9: Exceptions For error/problem situations Exception classes –ArithmeticException, IOException, etc. –checked exceptions try blocks –catch statements.
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.
Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects CS 146 Class Notes Fall 10.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Exceptions Chapter 10 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
© 2004 Pearson Addison-Wesley. All rights reserved April 17, 2006 Polymorphism (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
© Mohamed Nuzrath Java Programming :: Chapter 6 :: Prepared & Presented By :: Mohamed Nuzrath [ Major In Programming ] NCC Programme coordinator IT Lecturer.
1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.
© 2004 Pearson Addison-Wesley. All rights reserved April 24, 2006 Exceptions (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING 2006.
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
Chapter 10 Exceptions 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
© 2004 Pearson Addison-Wesley. All rights reserved December 5, 2007 I/O Exceptions & Working with Files ComS 207: Programming I (in Java) Iowa State University,
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
CSE 1201 Object Oriented Programming
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Chapter 10 – Exception Handling
10 Exceptions Software Solutions Lewis & Loftus java 5TH EDITION
Review for the Final Exam
ATS Application Programming: Java Programming
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Abdulmotaleb El Saddik University of Ottawa
Exception Handling Chapter 9 Edited by JJ.
Exceptions April 19, 2006 ComS 207: Programming I (in Java)
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
I/O Exceptions & Working with Files
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

© 2004 Pearson Addison-Wesley. All rights reserved November 30, 2007 Exceptions ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor: Alexander Stoytchev

© 2004 Pearson Addison-Wesley. All rights reserved Quick Review of Polymorphism & Polymorphic Variables

© 2004 Pearson Addison-Wesley. All rights reserved Class Hierarchy A C B DEFG H I J K L M N O

© 2004 Pearson Addison-Wesley. All rights reserved Class Hierarchy A C B DEFG H I J K L M N O type = A

© 2004 Pearson Addison-Wesley. All rights reserved Class Hierarchy A C B DEFG H I J K L M N O type = A type = A & B

© 2004 Pearson Addison-Wesley. All rights reserved Class Hierarchy A C B DEFG H I J K L M N O type = A type = A & B type = A & B & D

© 2004 Pearson Addison-Wesley. All rights reserved Class Hierarchy A C B DEFG H I J K L M N O type = A type = A & B type = A & B & D type = A & B & D & I

© 2004 Pearson Addison-Wesley. All rights reserved Class Hierarchy A C B DEFG H I J K L M N O type = A type = A & B & D & H type = A & B & E & K

© 2004 Pearson Addison-Wesley. All rights reserved Class Hierarchy A C B DEFG H I J K L M N O type = A type = A & B & D type = A & C

© 2004 Pearson Addison-Wesley. All rights reserved Example: Letters class hierarchy A.java B.java C.java D.java E.java F.java G.java Driver.java

© 2004 Pearson Addison-Wesley. All rights reserved public abstract class A { abstract void name(); } public class B extends A { public void name() { System.out.println(“B"); } public void Bstuff() { System.out.println(“BB"); } public class C extends A { public void name() { System.out.println(“C"); } public void Cname() { System.out.println(“CC"); }

© 2004 Pearson Addison-Wesley. All rights reserved public abstract class A { abstract void name(); } public class B extends A { public void name() { System.out.println(“B"); } public void Bstuff() { System.out.println(“BB"); } public class C extends A { public void name() { System.out.println(“C"); } public void Cname() { System.out.println(“CC"); } public class D extends B { public void name() { System.out.println(“D"); } public void Bstuff() { System.out.println(“DD"); } public void Dstuff() { System.out.println(“DDD"); } public class E extends B { public void name() { System.out.println(“E"); } public void Bstuff() { System.out.println(“EE"); } public void Estuff() { System.out.println(“EEE"); }

© 2004 Pearson Addison-Wesley. All rights reserved public abstract class A { abstract void name(); } public class B extends A { public void name() { System.out.println(“B"); } public void Bstuff() { System.out.println(“BB"); } public class C extends A { public void name() { System.out.println(“C"); } public void Cname() { System.out.println(“CC"); } public class F extends C { public void name() { System.out.println(“F"); } public void Cstuff() { System.out.println(“FF"); } public void Fstuff() { System.out.println(“FFF"); } public class G extends C { public void name() { System.out.println(“G"); } public void Cstuff() { System.out.println(“GG"); } public void Gstuff() { System.out.println(“GGG"); }

© 2004 Pearson Addison-Wesley. All rights reserved public class Driver1 { public static void main(String[] args) { A[] letter= new A[6]; letter[0]= new B(); letter[1]= new D(); letter[2]= new E(); letter[3]= new C(); letter[4]= new F(); letter[5]= new G(); for(int i=0; i<6; i++) { letter[i].name (); } } } Result: B D E C F G

© 2004 Pearson Addison-Wesley. All rights reserved public class Driver2 { public static void main(String[] args) { A[] letter= new A[6]; letter[0]= new B(); letter[1]= new D(); letter[2]= new E(); letter[3]= new C(); letter[4]= new F(); letter[5]= new G(); for(int i=0; i<3; i++) { ((B)letter[i]).Bstuff(); } for(int i=3; i<6; i++) { ((C)letter[i]).Cstuff();} } Result: BB DD EE CC FF GG

© 2004 Pearson Addison-Wesley. All rights reserved public class Driver3 { public static void main(String[] args) { B[] b_letter= new B[3]; b_letter[0]= new B(); b_letter[1]= new D(); b_letter[2]= new E(); C[] c_letter= new C[3]; c_letter[0]= new C(); c_letter[1]= new F(); c_letter[2]= new G(); for(int i=0; i<3; i++) { b_letter[i].Bstuff(); } for(int i=0; i<3; i++) { c_letter[i].Cstuff(); } } Result: BB DD EE CC FF GG

© 2004 Pearson Addison-Wesley. All rights reserved Quick Review of Last Lecture

© 2004 Pearson Addison-Wesley. All rights reserved Example: Animals class hierarchy Animal.java Cow.java Duck.java Dog.java Farm.java

© 2004 Pearson Addison-Wesley. All rights reserved You can use jGrasp to draw diagram like this one

© 2004 Pearson Addison-Wesley. All rights reserved Class Hierarchy

© 2004 Pearson Addison-Wesley. All rights reserved public abstract class Animal { abstract void makeSound(); } public class Cow extends Animal { public void makeSound() { System.out.println("Moo-Moo"); } } public class Dog extends Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } } public class Duck extends Animal { public void makeSound() { System.out.println(“Quack-Quack"); } }

© 2004 Pearson Addison-Wesley. All rights reserved public class Farm { public static void main(String[] args) { Cow c=new Cow(); Dog d=new Dog(); Duck k= new Duck(); c.makeSound(); d.makeSound(); k.makeSound(); } Result: Moo-Moo Wuf-Wuf Quack-Quack

© 2004 Pearson Addison-Wesley. All rights reserved public class Farm2 { public static void main(String[] args) { Animal[] a = new Animal[3]; a[0] = new Cow(); a[1] = new Dog(); a[2] = new Duck(); for(int i=0; i< a.length; i++) a[i].makeSound(); } } Result: Moo-Moo Wuf-Wuf Quack-Quack

© 2004 Pearson Addison-Wesley. All rights reserved a

a Not possible since Animal is abstract

© 2004 Pearson Addison-Wesley. All rights reserved But if we add more classes to the Class hierarchy

© 2004 Pearson Addison-Wesley. All rights reserved a We can do this…

© 2004 Pearson Addison-Wesley. All rights reserved public abstract class Animal { abstract void makeSound(); public void move() { System.out.println(“walk”); } } public class Cow extends Animal { public void makeSound() { System.out.println("Moo-Moo"); } } public class Dog extends Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } } public class Duck extends Animal { public void makeSound() { System.out.println(“Quack-Quack"); } } Define a new method called move(). It is not abstract and will be inherited by all children of Animal.

© 2004 Pearson Addison-Wesley. All rights reserved public class Farm2b { public static void main(String[] args) { Animal[] a = new Animal[3]; a[0] = new Cow(); a[1] = new Dog(); a[2] = new Duck(); for(int i=0; i< a.length; i++) a[i].move(); } } Result: walk

© 2004 Pearson Addison-Wesley. All rights reserved public abstract class Animal { abstract void makeSound(); public void move() { System.out.println(“walk”); } } public class Cow extends Animal { public void makeSound() { System.out.println("Moo-Moo"); } } public class Dog extends Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } } Override the move method defined in the Animal class. public class Duck extends Animal { public void makeSound() { System.out.println(“Quack-Quack"); } public void move() { System.out.println(“fly”); } }

© 2004 Pearson Addison-Wesley. All rights reserved public class Farm2c { public static void main(String[] args) { Animal[] a = new Animal[3]; a[0] = new Cow(); a[1] = new Dog(); a[2] = new Duck(); for(int i=0; i< a.length; i++) a[i].move(); } } Result: Walk Fly

Chapter 9 Section 9.1 & 9.2

© 2004 Pearson Addison-Wesley. All rights reserved public abstract class Animal { abstract void makeSound(); public void move() { System.out.println(“walk”); } } public class Cow extends Animal { public void makeSound() { System.out.println("Moo-Moo"); } } public class Dog extends Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } } Only Ducks can dive. public class Duck extends Animal { public void makeSound() { System.out.println(“Quack-Quack"); } public void move() { System.out.println(“fly”); } public void dive() { System.out.println(“Diving…"); } }

© 2004 Pearson Addison-Wesley. All rights reserved public class Farm2d { public static void main(String[] args) { Animal[] a = new Animal[3]; a[0] = new Cow(); a[1] = new Dog(); a[2] = new Duck(); for(int i=0; i< a.length; i++) a[i].dive(); } } Compile Error, since dive() is defined only for Duck objects and not for all objects derived from Animal.

© 2004 Pearson Addison-Wesley. All rights reserved public class Farm2d { public static void main(String[] args) { Animal[] a = new Animal[3]; a[0] = new Cow(); a[1] = new Dog(); a[2] = new Duck(); ((Duck)a[2]).dive(); } } This works OK, but requires a cast from a reference to Animal to a reference to Duck.

© 2004 Pearson Addison-Wesley. All rights reserved public class Farm2d { public static void main(String[] args) { Animal[] a = new Animal[3]; a[0] = new Cow(); a[1] = new Dog(); a[2] = new Duck(); ((Duck)a[2]).dive(); } } Result: Diving…

Chapter 9 Section 9.3

© 2004 Pearson Addison-Wesley. All rights reserved The Animals example with interfaces implements In this case Animal is an interface.

© 2004 Pearson Addison-Wesley. All rights reserved public interface Animal { public void makeSound(); } public class Cow implements Animal { public void makeSound() { System.out.println("Moo-Moo"); } } public class Dog implements Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } } public class Duck implements Animal { public void makeSound() { System.out.println(“Quack-Quack"); } }

© 2004 Pearson Addison-Wesley. All rights reserved public class iFarm { public static void main(String[] args) { Animal domestic; domestic = new Cow(); domestic.makeSound(); domestic = new Dog(); domestic.makeSound(); domestic = new Duck(); domestic.makeSound(); } Result: Moo-Moo Wuf-Wuf Quack-Quack

© 2004 Pearson Addison-Wesley. All rights reserved public interface Animal { public void makeSound(); public void move(); } public class Cow implements Animal { public void makeSound() { System.out.println("Moo-Moo"); } public void move() { System.out.println(“walk"); } } public class Dog implements Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } public void move() { System.out.println(“walk"); } } Define a new method called move(). Because Animal is an interface this method cannot be defined as in the previous example in which Animal was an abstract class. public class Duck implements Animal { public void makeSound() { System.out.println(“Quack-Quack"); } public void move() { System.out.println(“fly"); } } Replicated code!!! The only move() method that is different from the rest.

© 2004 Pearson Addison-Wesley. All rights reserved public class iFarm2 { public static void main(String[] args) { Animal domestic; domestic = new Cow(); domestic.move(); domestic = new Dog(); domestic.move(); domestic = new Duck(); domestic.move(); } Result: walk fly

© 2004 Pearson Addison-Wesley. All rights reserved public interface Animal { public void makeSound(); public void move(); } public class Cow implements Animal { public void makeSound() { System.out.println("Moo-Moo"); } public void move() { System.out.println(“walk"); } } public class Dog implements Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } public void move() { System.out.println(“walk"); } } public class Duck implements Animal { public void makeSound() { System.out.println(“Quack-Quack"); } public void move() { System.out.println(“fly"); } public void dive() { System.out.println(“Diving…"); } } Only Ducks can dive().

© 2004 Pearson Addison-Wesley. All rights reserved public class iFarm3 { public static void main(String[] args) { Animal domestic; domestic = new Cow(); //domestic.dive(); // error domestic = new Dog(); //domestic.dive(); // error domestic = new Duck(); // domestic.dive(); // error ((Duck)domestic).dive(); // OK, but uses a cast } } Result: Ducks can dive.

Chapter 10 Exceptions

© 2004 Pearson Addison-Wesley. All rights reserved Exceptions An exception is an object that describes an unusual or erroneous situation.

Chapter 10 Sections

© 2004 Pearson Addison-Wesley. All rights reserved Exceptions Exceptions are thrown by a program, and may be caught and handled by another part of the program A program can be separated into a normal execution flow and an exception execution flow An error is also represented as an object in Java, but usually represents a unrecoverable situation and should not be caught

© 2004 Pearson Addison-Wesley. All rights reserved Exception Handling Java has a predefined set of exceptions and errors that can occur during execution A program can deal with an exception in one of three ways:  ignore it  handle it where it occurs  handle it an another place in the program The manner in which an exception is processed is an important design consideration

© 2004 Pearson Addison-Wesley. All rights reserved Exception Handling If an exception is ignored by the program, the program will terminate abnormally and produce an appropriate message The message includes a call stack trace that:  indicates the line on which the exception occurred  shows the method call trail that lead to the attempted execution of the offending line See Zero.java (page 533) Zero.java

© 2004 Pearson Addison-Wesley. All rights reserved Examples: Zero.java Zero_Caught.java

© 2004 Pearson Addison-Wesley. All rights reserved The Exception Class Hierarchy Classes that define exceptions are related by inheritance, forming an exception class hierarchy All error and exception classes are descendents of the Throwable class A programmer can define an exception by extending the Exception class or one of its descendants The parent class used depends on how the new exception will be used

© 2004 Pearson Addison-Wesley. All rights reserved Exceptions Class Hierarchy

© 2004 Pearson Addison-Wesley. All rights reserved Exception Hierarchy java.lang.Object java.lang.Throwable java.lang.Exception java.lang.RuntimeException java.lang.ArithmeticException

© 2004 Pearson Addison-Wesley. All rights reserved The try Statement To handle an exception in a program, the line that throws the exception is executed within a try block A try block is followed by one or more catch clauses Each catch clause has an associated exception type and is called an exception handler When an exception occurs, processing continues at the first catch clause that matches the exception type

© 2004 Pearson Addison-Wesley. All rights reserved The finally Clause A try statement can have an optional clause following the catch clauses, designated by the reserved word finally The statements in the finally clause always are executed If no exception is generated, the statements in the finally clause are executed after the statements in the try block complete If an exception is generated, the statements in the finally clause are executed after the statements in the appropriate catch clause complete

© 2004 Pearson Addison-Wesley. All rights reserved Examples: OutOfBounds.java OutOfBounds_Caught.java

© 2004 Pearson Addison-Wesley. All rights reserved Exception Hierarchy java.lang.Object java.lang.Throwable java.lang.Exception java.lang.RuntimeException java.lang.IndexOutOfBoundsException java.lang.ArrayIndexOutOfBoundsException

© 2004 Pearson Addison-Wesley. All rights reserved Examples: NullReference.java NullReference_Caught.java

© 2004 Pearson Addison-Wesley. All rights reserved Exception Hierarchy java.lang.Object java.lang.Throwable java.lang.Exception java.lang.RuntimeException java.lang.NullPointerException

© 2004 Pearson Addison-Wesley. All rights reserved Examples: ClassCast.java ClassCast_Caught.java

© 2004 Pearson Addison-Wesley. All rights reserved Exception Hierarchy java.lang.Object java.lang.Throwable java.lang.Exception java.lang.RuntimeException java.lang.ClassCastException

© 2004 Pearson Addison-Wesley. All rights reserved On-line Java Documentation

© 2004 Pearson Addison-Wesley. All rights reserved THE END