©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 1 Chapter 8 Exception Handling Sections 1-5, 7.

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

©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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Exception Handling and Format output
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.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions Chapter 18 Programs: DriverException2 DriverException TestAgeInputException.
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 Chapter 8 Exceptions and Assertions.
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.
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.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
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.
©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.
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.
CS203 Java Object Oriented Programming Errors and Exception Handling.
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.
Preventing and Correcting Errors
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.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
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
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 –
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.
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.
CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
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.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
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.
DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exception Handling.
Exceptions in the Java programming language J. W. Rider.
Introduction to OOP with Java 4th Ed, C. Thomas Wu
Java Exceptions a quick review….
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 Edited by JJ.
Chapter 6-3 (Book Chapter 8)
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

©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 Definition An exception represents an error condition that occurs during the normal course of program execution. –Exceptions are error conditions that are not the fault of the programmer When an exception occurs, (we say it is thrown), the normal sequence of flow is terminated. When an exception-handling routine is executed, we say the exception is caught.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Programming with Exceptions As a programmer, you have control over what happens when an exception is thrown –If you do nothing, the program will quit –You can add code to recover from the exception where it occurs in any method on the call stack when the exception is thrown You can create and throw exceptions when errors occur in the program You can create your own exception classes –Extend Exception or RuntimeException

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Ignoring 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

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Catching an Exception trycatch inputStr = JOptionPane.showInputDialog(null, "Age:"); try { age = Integer.parseInt(inputStr); } catch (NumberFormatException e){ JOptionPane.showMessageDialog(null, "’" + inputStr + "‘ is invalid\n" + "Please enter digits only"); }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter try-catch Control Flow

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Exception Methods The Exception class has two methods we can call to get information about the thrown exception: –getMessage returns a String describing the error –printStackTrace returns a list of all the methods on the call stack and the line numbers being executed try {... } catch (NumberFormatException e){ System.out.println(e.getMessage()); e.printStackTrace(); }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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){... }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Multiple catch Control Flow

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter try-catch-finally Control Flow

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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; }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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 value");... }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Sample Call Sequence

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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. There are over 60 classes in the hierarchy.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Checked vs. Runtime Exceptions A checked exception is an exception that is checked at compile time. –The compiler requires that you either have a try-catch statement or a throws clause –ParseException is a checked exception All other exceptions are unchecked, or runtime, exceptions. As the name suggests, they are detected only at runtime. –You don't have to handle runtime exceptions unless you want to.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Handling Checked Exceptions

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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 might define an 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.