DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exception Handling.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 8 Exceptions and Assertions.
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
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.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
Exception Handling and Format output
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.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to –Improve the reliability.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
 2005 Pearson Education, Inc. All rights reserved Exception Handling.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions Chapter 18 Programs: DriverException2 DriverException TestAgeInputException.
Chapter Chapter 8 Exceptions and Assertions.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
E XCEPTION H ANDLING Chapter 11 C S 442: A DVANCED J AVA P ROGRAMMING.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Exception handling Dealing with life’s little surprises.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions Handling.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions and Assertions Animated Version.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Ahmad Al-Rjoub Chapter 6 Exceptions CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science
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 Software Solutions Foundations of Program Design Sixth Edition
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Exception Handling and Format output. Midterm exam Date: Nov 1 st, 2006 Content: Week 1 to Week 8 Format: Multiple choice Determine the results of the.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
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
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
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.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
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.
Unit 4 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings.
EXCEPTIONS There's an exception to every rule.. 2 Introduction: Methods  The signature of a method includes  access control modifier  return type 
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Introduction to OOP with Java 4th Ed, C. Thomas Wu
Chapter 10 – Exception Handling
CS1101: Programming Methodology Recitation 7 – Exceptions
Exception Handling and Format output-CS1050-By Gayani Gupta
Chapter 6-3 (Book Chapter 8)
Exception Handling Chapter 9.
Abdulmotaleb El Saddik University of Ottawa
Lecture 11 Objectives Learn what an exception is.
Chapter 6-3 (Book Chapter 8)
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exception Handling

DCS 2133 Object Oriented Programming Objectives To understand the concept of exception handling when program execution errors occur. To implement exception handling in program execution.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction A program can be written assuming that nothing unusual or incorrect will happen. –The user will always enter an integer when prompted to do so. –There will always be a nonempty list for a program that takes an entry from the list. –The file containing the needed information will always exist. Unfortunately, it isn’t always so.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction Once the core program is written for the usual, expected case(s), Java’s exception-handling facilities should be added to accommodate the unusual, unexpected case(s). Exception handling divides a class or method definition into separate sections: –one section for the normal case(s) –another section for the exceptional case(s). Depending on how a method is used, special cases may need to be handled in different ways.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exception What is exception? An error condition that can occur during the normal course of program execution. Exception handling – resolving exceptions that may occur so program can continue or terminate gracefully Exception handling enables programmers to create programs that are more robust and fault- tolerant.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exception handling When an exception occurs, or is thrown, the normal sequence of flow is terminated. The exception-handling routine is then executed; we say the thrown exception is caught.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example of No Catching Exceptions String inputStr; int age; inputStr = JOptionPane.showInputDialog(null, "Age:"); age = Integer.parseInt(inputStr); java.lang.NumberFormatException: ten at java.lang.Integer.parseInt(Integer.java:405) at java.lang.Integer.parseInt(Integer.java:454) at Ch8Sample1.main(Ch8Sample1.java:20) Error message for invalid input

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions in Java: Example Simple example –Suppose the students are hosting an event to express appreciation to their lecturer, at which donuts and milk will be served. –If the number of donuts is known, and the number of glasses of milk is known, it should be possible to calculate the number of donuts per class of milk.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions in Java: Example, cont. Simple example, cont. –But what if there is no milk because the cows are on strike? –An attempt to divide the number of donuts by the number of glasses of milk will result in an attempt to divide by zero. –This would be an complete disaster, known in Java as an exception.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions in Java: Example, cont. In Java, it is possible to test for this unusual situation using an if-else statement, for example. class GotMilk

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions in Java: Example, cont.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions in Java: Example, cont. In Java, it is also possible to throw an exception. class ExceptionDemo

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions in Java: Example, cont.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions in Java: Example, cont.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions in Java Exceptions are handled using a try-throw-catch threesome. try block syntax try { Code_to_Try Throw_An_Exception_Or_Invoke_A_Method _That_Might_Throw_An_Exception Possibly_More_Code } try { Code_to_Try Throw_An_Exception_Or_Invoke_A_Method _That_Might_Throw_An_Exception Possibly_More_Code }

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions in Java try block – encloses code that might throw an exception and the code that should not execute if an exception occurs Consists of keyword try followed by a block of code enclosed in curly braces

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Catching Exceptions catch block – catches (i.e., receives) and handles an exception, contains: –Begins with keyword catch –Exception parameter in parentheses – exception parameter identifies the exception type and enables catch block to interact with caught exception object –Block of code in curly braces that executes when exception of proper type occurs

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Catching an exception using try – catch block inputStr = JOptionPane.showInputDialog(null, "Age:"); try { age = Integer.parseInt(inputStr); } catch (NumberFormatException e){ JOptionPane.showMessageDialog(null, "’" + inputStr + "‘ is invalid\n" + "Please enter digits only"); } trycatch

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Remaining statements in the try block is skipped. try {... } catch (Exception e) {... } try-catch Control Flow Assume throws an exception. Assume throws an exception. Exception Statements in the catch block are executed. And the execution continues to the next statement try {... } catch (Exception e) {... } No Exception Statements in the catch block are skipped. All statements in the try block are executed.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Using try-catch statement in a loop while (true){ inputStr = JOptionPane.showInputDialog(null, "Age:"); try { age = Integer.parseInt(inputStr); return age; } catch (NumberFormatException e){ JOptionPane.showMessageDialog(null, "’" + inputStr + "‘ is invalid\n" + "Please enter digits only"); } trycatch

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Another Example – Divide by Zero Exception public static int quotient( int numerator, int denominator ) { return numerator / denominator; // possible division by zero } public static void main( String args[] ) { Scanner scanner = new Scanner( System.in ); System.out.print( "Please enter an integer numerator: " ); int numerator = scanner.nextInt(); System.out.print( "Please enter an integer denominator: " ); int denominator = scanner.nextInt(); int result = quotient( numerator, denominator ); System.out.printf( "\nResult: %d / %d = %d\n", numerator, denominator, result ); }

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Information There are two methods we can call to get information about the thrown exception: –getMessage –printStackTrace try {... } catch (NumberFormatException e){ System.out.println(e.getMessage()); System.out.println(e.printStackTrace()); } ten java.lang.NumberFormatException: ten at java.lang.Integer.parseInt.... getMessage printStacktrace

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Multiple catch Blocks A single try-catch statement can include multiple catch blocks, one for each type of exception. try {... age = Integer.parseInt(inputStr);... val = cal.get(id); //cal is a GregorianCalendar... } catch (NumberFormatException e){... } catch (ArrayIndexOutOfBoundsException e){... }

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. All catch blocks are skipped. try {... }... } No Exception Multiple catch Control Flow All statements in the try block are executed and throw no exceptions. Remaining statements in the try block is skipped. try {... }... } Assume throws an exception and is the matching block. Assume throws an exception and is the matching block. Exception Statements in the matching catch block are executed.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The finally Block There are situations where we need to take certain actions regardless of whether an exception is thrown or not. We place statements that must be executed regardless of exceptions in the finally block.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. try-catch-finally Control Flow No Exception try { } } finally {... } Assume throws an exception and is the matching block. Assume throws an exception and is the matching block. Exception try { } } finally {... } finally block is executed.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Propagating Exceptions Instead of catching a thrown exception by using the try- catch statement, we can propagate the thrown exception back to the caller of our method. The method header includes the reserved word throws. public int getAge( ) throws NumberFormatException {... int age = Integer.parseInt(inputStr);... return age; }

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Throwing Exceptions We can write a method that throws an exception directly, i.e., this method is the origin of the exception. Use the throw reserved to create a new instance of the Exception or its subclasses. The method header includes the reserved word throws. public void doWork(int num) throws Exception {... if (num != val) throw new Exception("Invalid val");... }

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exception Thrower When a method may throw an exception, either directly or indirectly, we call the method an exception thrower. Every exception thrower must be one of two types: –catcher. –propagator.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Types of Exception Throwers An exception catcher is an exception thrower that includes a matching catch block for the thrown exception. An exception propagator does not contain a matching catch block. A method may be a catcher of one exception and a propagator of another.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sample Call Sequence try { B(); } catch (Exception e){... } Method A try { C(); } catch (Exception e){... } Method B try { D(); } catch (Exception e){... Method C if (cond) { throw new Exception(); Method D propagator catcher AA B A B C A B C D Stack Trace

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exception Types All types of thrown errors are instances of the Throwable class or its subclasses. Serious errors are represented by instances of the Error class or its subclasses. Exceptional cases that common applications should handle are represented by instances of the Exception class or its subclasses.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Throwable Hierarchy There are over 60 classes in the hierarchy.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Checked vs. Runtime There are two types of exceptions: –Checked. –Unchecked. A checked exception is an exception that is checked at compile time. All other exceptions are unchecked, or runtime, exceptions. As the name suggests, they are detected only at runtime.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Different Handling Rules When calling a method that can throw checked exceptions –use the try-catch statement and place the call in the try block, or –modify the method header to include the appropriate throws clause. When calling a method that can throw runtime exceptions, it is optional to use the try-catch statement or modify the method header to include a throws clause.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Handling Checked Exceptions

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Handling Runtime Exceptions

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Programmer-Defined Exceptions Using the standard exception classes, we can use the getMessage method to retrieve the error message. By defining our own exception class, we can pack more useful information –for example, we may define a OutOfStock exception class and include information such as how many items to order AgeInputException is defined as a subclass of Exception and includes public methods to access three pieces of information it carries: lower and upper bounds of valid age input and the (invalid) value entered by the user.

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example – AgeInputException class AgeInputException extends Exception { private static final String DEFAULT_MESSAGE = "Input out of bounds"; private int lowerBound; private int upperBound; private int value; public AgeInputException(int low, int high, int input) { this(DEFAULT_MESSAGE, low, high, input); } public AgeInputException(String msg, int low, int high, int input) { super(msg); if (low > high) { throw new IllegalArgumentException(); } lowerBound = low; upperBound = high; value = input; } class AgeInputException extends Exception { private static final String DEFAULT_MESSAGE = "Input out of bounds"; private int lowerBound; private int upperBound; private int value; public AgeInputException(int low, int high, int input) { this(DEFAULT_MESSAGE, low, high, input); } public AgeInputException(String msg, int low, int high, int input) { super(msg); if (low > high) { throw new IllegalArgumentException(); } lowerBound = low; upperBound = high; value = input; } public int lowerBound() { return lowerBound; } public int upperBound() { return upperBound; } public int value() { return value; } public int lowerBound() { return lowerBound; } public int upperBound() { return upperBound; } public int value() { return value; }

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example – AgeInputVer5 import javax.swing.*; class AgeInputVer5 { private static final String DEFAULT_MESSAGE = "Your age:"; private static final int DEFAULT_LOWER_BOUND = 0; private static final int DEFAULT_UPPER_BOUND = 99; private int lowerBound; private int upperBound; public AgeInputVer5( ) throws IllegalArgumentException { setBounds(DEFAULT_LOWER_BOUND, DEFAULT_UPPER_BOUND); } public AgeInputVer5(int low, int high) throws IllegalArgumentException { if (low > high) { throw new IllegalArgumentException( "Low (" + low + ") was " + "larger than high(" + high + ")"); } else { setBounds(low, high); } public int getAge() throws AgeInputException { return getAge(DEFAULT_MESSAGE); } import javax.swing.*; class AgeInputVer5 { private static final String DEFAULT_MESSAGE = "Your age:"; private static final int DEFAULT_LOWER_BOUND = 0; private static final int DEFAULT_UPPER_BOUND = 99; private int lowerBound; private int upperBound; public AgeInputVer5( ) throws IllegalArgumentException { setBounds(DEFAULT_LOWER_BOUND, DEFAULT_UPPER_BOUND); } public AgeInputVer5(int low, int high) throws IllegalArgumentException { if (low > high) { throw new IllegalArgumentException( "Low (" + low + ") was " + "larger than high(" + high + ")"); } else { setBounds(low, high); } public int getAge() throws AgeInputException { return getAge(DEFAULT_MESSAGE); } public int getAge(String prompt) throws AgeInputException { String inputStr; int age; while (true) { inputStr = JOptionPane.showInputDialog(null, prompt); try { age = Integer.parseInt(inputStr); if (age upperBound) { throw new AgeInputException("Input out of bound", lowerBound, upperBound, age); } return age; //input okay so return the value & exit } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "'" + inputStr + "' is invalid\n“ + "Please enter digits only"); } private void setBounds(int low, int high) { lowerBound = low; upperBound = high; } public int getAge(String prompt) throws AgeInputException { String inputStr; int age; while (true) { inputStr = JOptionPane.showInputDialog(null, prompt); try { age = Integer.parseInt(inputStr); if (age upperBound) { throw new AgeInputException("Input out of bound", lowerBound, upperBound, age); } return age; //input okay so return the value & exit } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "'" + inputStr + "' is invalid\n“ + "Please enter digits only"); } private void setBounds(int low, int high) { lowerBound = low; upperBound = high; }

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example -AgeInputExceptionDemo import javax.swing.*; class Ch8TestAgeInputVer5 { public static void main( String[] args ) { int entrantAge; try { AgeInputVer5 input = new AgeInputVer5(25, 50); entrantAge = input.getAge("Your Age:"); //continue the processing JOptionPane.showMessageDialog(null, "Input Okay"); //TEMP } catch (AgeInputException e) { JOptionPane.showMessageDialog(null, "Error: " + e.value() + " is entered. It is " + "outside the valid range of [" + e.lowerBound() + ", " + e.upperBound() + "]"); } import javax.swing.*; class Ch8TestAgeInputVer5 { public static void main( String[] args ) { int entrantAge; try { AgeInputVer5 input = new AgeInputVer5(25, 50); entrantAge = input.getAge("Your Age:"); //continue the processing JOptionPane.showMessageDialog(null, "Input Okay"); //TEMP } catch (AgeInputException e) { JOptionPane.showMessageDialog(null, "Error: " + e.value() + " is entered. It is " + "outside the valid range of [" + e.lowerBound() + ", " + e.upperBound() + "]"); }

DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sample output Enter 0 Will produce