16 Exception Handling.

Slides:



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

Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
CSE 1302 Lecture 21 Exception Handling and Parallel Programming Richard Gesick.
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.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
 2005 Pearson Education, Inc. All rights reserved Exception Handling.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Exception Handling: A Deeper.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
C++ Exception Handling
E XCEPTION H ANDLING Chapter 11 C S 442: A DVANCED J AVA P ROGRAMMING.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo.
 2002 Prentice Hall. All rights reserved Exception-Handling Overview Exception handling –improves program clarity and modifiability by removing.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
 2009 Pearson Education, Inc. All rights reserved Exception Handling.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Dr. Abraham. Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Pemrograman VisualMinggu …12… Page 1 MINGGU Ke Duabelas Pemrograman Visual Pokok Bahasan: Exception Handling Tujuan Instruksional Khusus: Mahasiswa dapat.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Unit 4 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Exception Handling in C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Lecture 18B Exception Handling and Richard Gesick.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
Appendix H Exception Handling: A Deeper Look
Exception Handling in C++
IS 0020 Program Design and Software Tools
Chapter 16 Exception Handling: A Deeper Look
EXCEPTION HANDLING IN C++
Java Programming Fifth Edition
Andy Wang Object Oriented Programming in C++ COP 3330
Why exception handling in C++?
Exception Handling and Format output-CS1050-By Gayani Gupta
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Object Oriented Programming COP3330 / CGS5409
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exception Handling and
Designing with Java Exception Handling
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 12 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.
Designing with Java Exception Handling
Fundaments of Game Design
16 Exception Handling.
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
Chapter 11: Exception Handling
Presentation transcript:

16 Exception Handling

16.1 Introduction Exceptions Exception handling Indicate problems that occur during a program’s execution Occur infrequently Exception handling Can resolve exceptions Allow a program to continue executing or Notify the user of the problem and Terminate the program in a controlled manner Makes programs robust and fault-tolerant

16.2 Exception-Handling Overview Intermixing program and error-handling logic Pseudocode example Perform a task If the preceding task did not execute correctly Perform error processing Perform next task If the preceding task did not execute correctly Perform error processing … Makes the program difficult to read, modify, maintain and debug

16.2 Exception-Handling Overview (Cont.) Removes error-handling code from the program execution’s “main line” Programmers can handle any exceptions they choose All exceptions, All exceptions of a certain type or All exceptions of a group of related types

16.3 Example: Handling an Attempt to Divide by Zero Class exception Is the standard C++ base class for all exceptions Provides its derived classes with virtual function what Returns the exception’s stored error message

Outline DivideBy ZeroException.h (1 of 1)

Outline Fig16_02.cpp (1 of 3)

Outline Fig16_02.cpp (2 of 3)

Outline Fig16_02.cpp (3 of 3)

16.3 Example: Handling an Attempt to Divide by Zero (Cont.) try Blocks Keyword try followed by braces ({}) Should enclose Statements that might cause exceptions and Statements that should be skipped in case of an exception

16.3 Example: Handling an Attempt to Divide by Zero (Cont.) catch handlers Immediately follow a try block One or more catch handlers for each try block Keyword catch Exception parameter enclosed in parentheses Represents the type of exception to process Can provide an optional parameter name to interact with the caught exception object Executes if exception parameter type matches the exception thrown in the try block Could be a base class of the thrown exception’s class

16.3 Example: Handling an Attempt to Divide by Zero (Cont.) Termination model of exception handling try block expires when an exception occurs Local variables in try block go out of scope The code within the matching catch handler executes Control resumes with the first statement after the last catch handler following the try block Control does not return to throw point Stack unwinding Occurs if no matching catch handler is found Program attempts to locate another enclosing try block in the calling function

16.3 Example: Handling an Attempt to Divide by Zero (Cont.) Throwing an exception Use keyword throw followed by an operand representing the type of exception The throw operand can be of any type If the throw operand is an object, it is called an exception object The throw operand initializes the exception parameter in the matching catch handler, if one is found

16.4 When to Use Exception Handling To process synchronous errors Occur when a statement executes Not to process asynchronous errors Occur in parallel with, and independent of, program execution To process problems arising in predefined software elements Such as predefined functions and classes Error handling can be performed by the program code to be customized based on the application’s needs