©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.

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.
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.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
Exception Handling and Format output
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.
Chapter 12 By Tony Gaddis Modified by Elizabeth Adams Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions Chapter 18 Programs: DriverException2 DriverException TestAgeInputException.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Chapter Chapter 8 Exceptions and Assertions.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
E XCEPTION H ANDLING Chapter 11 C S 442: A DVANCED J AVA P ROGRAMMING.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Exception handling Dealing with life’s little surprises.
©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.
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.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions and Assertions Animated Version.
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.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
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
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
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
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.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
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.
Java Programming: Guided Learning with Early Objects
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.
Unit 4 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Introduction to Exceptions in Java CS201, SW Development Methods.
DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exception Handling.
Generics, Exception and Undo Command. A Generic Stack Class AStack uses generic. In the main method, we have 2 instances of AStack, each type is Integer.
Exceptions in the Java programming language J. W. Rider.
Introduction to OOP with Java 4th Ed, C. Thomas Wu
Java Exceptions a quick review….
Exception Handling and Format output-CS1050-By Gayani Gupta
Chapter 14: Exception Handling
Presentation transcript:

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class Handling Exceptions Throwing Exceptions Exception propagation

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Exceptions  An exception represents an error condition that can occur during the normal course of program execution.  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. see Ch8Sample1

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Throwable Class  In Java an exception is represented as an instance of the Throwable class or its subclasses.  The Throwable class has two subclasses: Error - the Error class represents serious problems that should not be caught by ordinary applications. Exception - the Exception class represents error conditions that can be caught and handled.

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Handling Exceptions  Until now, we've allowed Exceptions to terminate our programs.  Programs are much more reliable and robust if we put in code that allows the program to recover from Exceptions.  One way to do this is to wrap the statements that may throw an exception with the try-catch control statement.

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 The try-catch statement  Try-catch is a kind of control statement that allows us to control what happens when an Exception occurs. try catch (Exception e)  We must specify which exception we are catching in the catch block’s parameter list.

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Example inputStr = JOptionPane.showInputDialog(null, prompt); 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. COMPSCI 125 Spring 2005 Execution Sequence  Statements in the try block are executed in sequence.  When one of the statements throws an exception, control is passed to the matching catch block and statements inside the catch block are executed.  The execution then continues to the statement following the try-block statement, ignoring any remaining statements in the try block.  If no statements in the try block throw an exception, the catch block is ignored. Execution continues with the statement following the try-catch statement.

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 try-catch Control Flow  try-catch statement with one catch block.

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Throwable Methods  There are two methods of the Throwable class we can call to get information about the thrown exception: getMessage - returns a description of the problem printStackTrace - prints the sequence of methods called to get to the line where the error occurred

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Run-Time Stack  The run-time stack is what is used to keep track of the methods that are currently being executed.  When you call a method, the system puts the parameter data and the location the method was called from on the top of a data structure called a stack.  When the method finishes, this data is removed from the stack and the calling method continues at the line the method was called from.

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Throwing Exceptions  An exception can be thrown in a program using the throw statement. throw where is an instance of the Throwable class or its subclasses.  This allows you to alter the flow of your program by creating and throwing Exceptions

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Catching Exceptions  A try statement can have multiple catch blocks.  When there are multiple catch blocks in a try- catch statement, they are checked in sequence. It is important to check more specialized exception classes before the more general exception classes.  When an exception is thrown, its matching catch block is executed and the other catch blocks are ignored.  Alternately, you can have a single catch block and use the instanceof operator to determine what type of Exception was thrown.

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 try-catch with multiple catch blocks

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Which catch Block?  If a catch block matches the Exception, it will be executed.  If none of the catch blocks matches the thrown exception, the system will search down the stack trace for a method with a matching catch block.  If no matching catch block is found, the system will handle the thrown exception.

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 The finally Clause  If there is a block of code that must be executed regardless of whether an exception is thrown, we use the reserved word finally.  The finally block is executed even if there is a return statement inside the try block. When the return statement is encountered in the try block, statements in the finally block are executed before actually returning from the method.

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 try-catch with finally

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Propagating Exceptions  It doesn't always make sense to handle an exception in the method where it occurs. Different users of a class may want to handle the same Exception differently. Do not catch an exception that is thrown as a result of violating a condition set by the client programmer.  Instead, propagate the exception back to the client programmer’s code and let him or her handle it.

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Propagating Exceptions  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: 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.

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Exception Propagation public A() { try { B() } catch (Exception e) { … } } public B() { try { C() } catch (Exception e) { … } } public C() { D();} public D() { if (cond)throw new Exception(); }

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Types of Exceptions  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 exceptions, Also known as runtime exceptions.

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 throws Clause  For a propagator of checked exceptions, we need to modify its header to declare the type of exceptions the method propagates. We use the reserved word throws for this declaration. void C( ) throws Exception {... } Without the required throws Exception clause, the program will not compile.  For runtime exceptions, the throws clause is optional.

©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Programmer-Defined Exceptions  Instead of using generic exception classes, we can define our own exception classes and attach useful information to the exception objects.  When creating a new customized exception class, define it as a checked exception to ensure client programmers handle thrown exceptions of this class in their code.