JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN 0136130887 © 2008 Pearson Education, Inc., Upper.

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

Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
CS102--Object Oriented Programming
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.
1 Week 11 l Basic Exception Handling »the mechanics of exceptions l Defining and Using Exceptions »some "simple" cases l Reality Check »guidelines for.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
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,
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Chapter 9 Exception Handling Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions Handling.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch Announcements l Project 6 now out. »Milestone due Oct. 24th »Final project.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 81 Exception Handling Chapter 8. 2 Objectives become familiar with the notion of exception handling learn Java syntax for exception handling learn.
Java Software Solutions Foundations of Program Design Sixth Edition
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Chapter 12: Exception Handling
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
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 © 2009 Pearson Education, Inc., Upper.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
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.
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 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
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, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Lecturer: Dr. AJ Bieszczad Chapter 8 COMP 150: Introduction to Object-Oriented Programming 8-1 l Basic Exception Handling »the mechanics of exceptions.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exception Handling Chapter 9.
Exception Handling Chapter 8 Basic Exception Handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling Chapter 9 Edited by JJ.
COS 260 DAY 20 Tony Gauvin.
Exception Handling Chapter 8 Basic Exception Handling
CMSC 202 Exceptions.
Presentation transcript:

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Walter Savitch Frank M. Carrano Walter Savitch Frank M. Carrano Exception Handling Chapter 9

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Objectives Describe the notion of exception handling React correctly when certain exceptions occur Use Java's exception-handling facilities effectively in classes and programs Use Java's predefined exception classes, and also define your own

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Exceptions in Java … An exception is an object that signals the occurrence of an unusual (“exceptional”) event during program execution Throwing an exception means creating an “exception object” Handling the exception requires code that detects and deals with the exception

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Exceptions in Java … Consider a program to assure us of a sufficient supply of milk View a possible solution in listing 9.1 class GotMilkpossible solution Sample screen output

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Exceptions in Java … Now we revise the program to use exception-handling View new version in listing 9.2 class ExceptionDemonew version Sample screen output 1 Sample screen output 2

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Exceptions in Java … Note the try block  It contains code where something could possibly go wrong  If something does go wrong, that’s where we throw an exception Note the catch block  When an exception is thrown, the catch block begins execution  A catch block is similar to a method with parameter, with the parameter being the thrown object

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Exceptions in Java … Note the flow of control when no exception is thrown View demo with no exception, listing 9.3 class ExceptionDemodemo with no exception Sample screen output with no exception

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Exceptions in Java Note the flow of control when exception is thrown View demo with exception, listing 9.4 class ExceptionDemodemo with exception Sample screen output when exception is thrown

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Predefined Exception Classes… Java has predefined exception classes within the Java Class Library Example classes  BadStringOperationException  ClassNotFoundException  IOException  NoSuchMethodException

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved …Predefined Exception Classes Example code

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Defining Your Own Exception Classes… Must be a derived class of some predefined exception class  Text uses classes derived from class Exception View sample class, listing 9.5 class DivideByZeroException extends Exceptionsample class View demo program, listing 9.6 class DivideByZeroDemodemo program

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Different runs of the program …Defining Your Own Exception Classes… Sample screen output 1 Sample screen output 2 Sample screen output 3

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved …Defining Your Own Exception Classes… Note method getMessage defined in exception classes  Returns string passed as argument to constructor  If no actual parameter used, default message returned The type of an object is the name of the exception class

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved …Defining Your Own Exception Classes Guidelines Use Exception as the base class Define at least two constructors  Default, no parameter  One-parameter with String parameter Start constructor definition with call to constructor of base class, using super Do not override inherited getMessage

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved More About Exception Classes: Outline Declaring Exceptions (Passing the Buck) Kinds of Exceptions Errors Multiple Throws and Catches The finally Block Rethrowing an Exception Case Study: A Line-Oriented Calculator

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Declaring Exceptions … Consider method where code throws exception  May want to handle immediately  May want to delay until something else is done Method that does not catch an exception  Notify programmers with throws clause  Programmer then given responsibility to handle exception

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Declaring Exceptions … Note syntax for throws clause Note distinction  Keyword throw used to throw exception  Keyword throws used in method heading to declare an exception

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Declaring Exceptions If a method throws exception and exception not caught inside the method  Method ends immediately after exception thrown A throws clause in overriding method  Can declare fewer exceptions than declared  But not more View program example, listing 9.7 class DoDivisionprogram example

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Kinds of Exceptions … In most cases, an exception is  Caught in a catch block … or  Declared in throws clause But Java has exceptions you do not need to account for Categories of exceptions  Checked exceptions  Unchecked exceptions

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Kinds of Exceptions … Checked exception  Must be caught in catch block  Or declared in throws clause Unchecked exception  Also called run-time exception  Need not be caught in catch block or declared in throws  Exceptions show that coding problems exist, and should be fixed

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Kinds of Exceptions … Examples of unchecked exceptions that might be thrown  Attempt to use array index out of bounds  Division by zero Uncaught runtime exception terminates program execution

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Kinds of Exceptions Figure 9.1 Hierarchy of the predefined exception classes

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Errors An error is an object of class Error  Similar to an unchecked exception  Need not catch or declare in throws clause  Object of class Error generated when abnormal conditions occur Errors are more or less beyond your control and require a change in the program to resolve

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Multiple Throws and Catches… A try block can throw any number of exceptions of different types Each catch block can catch exceptions of only one type The order of the catch blocks matters View example program, listing 9.8 class TwoCatchesDemoexample program View exception class used, listing 9.9 class NegativeNumberExceptionexception class

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved …Multiple Throws and Catches… Note multiple sample runs Sample screen output 1 Sample screen output 2 Sample screen output 3

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved …Multiple Throws and Catches One use of exceptions can be to deal with invalid user input When handling an exception thrown by a method, i t does not matter where in the method the throw occurred Use of a throw statement be should be reserved for situations where it is unavoidable The text suggests separate methods for throwing and catching of exceptions Nested try-catch blocks are possible but rarely useful

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The finally Block Possible to add a finally block after sequence of catch blocks The code in a finally block is executed  Whether or not an exception is thrown  Whether or not the required catch exists

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Rethrowing an Exception It is legal to throw an exception within a catch block It is possible to use the contents of a String parameter to throw either the same or a different type exception

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Case Study … A Line-Oriented Calculator  We want it to do addition, subtraction, multiplication and division  It will use line input/output  The user will enter: operation space number Enter  The calculator will then display the result

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Case Study … Proposed initial methods  Method to reset value of result to zero  Method to evaluate result of one operation  Method doCalculation to perform series of operations  Accessor method getResult : returns value of instance variable result  Mutator method setResult : sets value of instance variable result

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Case Study … View exception class, listing 9.10 class UnknownOpExceptionexception class View first version of calculator, listing 9.11 class PreLimCalculatorversion of calculator Sample screen output

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Case Study … Final version adds exception handling Ways to handle unknown operator  Catch exception in method evaluate  Let evaluate throw exception, catch exception in doCalculation  Let evaluate, doCalculation both throw exception, catch in main Latter option chosen

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Case Study View final version, listing 9.12 class Calculatorfinal version Sample screen output

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Summary … An exception is an object derived from class Exception  Descendants of class Error behave like exceptions Exception handling allows design of normal cases separate from exceptional situations Two kinds of exceptions  Checked and unchecked

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Summary … Exceptions can be thrown by  Java statements  Methods from class libraries  Programmer use of throw statement Method that might throw but not catch an exception should use throws clause Exception is caught in catch block

JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved … Summary A try block followed by one or more catch blocks  More specific exception catch types should come first Every exception type has getMessage method usable to recover description of caught description Do not overuse exceptions