Exception Handling Fall 2008 Dr. David A. Gaitros

Slides:



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

Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
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.
Exceptions OO Software Design and Construction Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ 1 Exceptions exceptiona program.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within.
CSIS 123A Lecture 11 Exception Handling. Introduction  Typical approach to development:  Write programs assuming things go as planned  Get ‘core’ working.
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 Exceptions and Exception Handling.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
Exception Handling. Introduction One benefit of C++ over C is its exception handling system. An exception is a situation in which a program has an unexpected.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
CS Advanced C++ Exception Handling Topic #5.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
計算機概論實習 A Simple Way to Handle Error if(b != 0) { cout
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.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
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.
Object Oriented Programming
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
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.
1 CSC241: Object Oriented Programming Lecture No 27.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
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.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
1 An Exception is… An unusual, often unpredictable event, detectable by software or hardware, that requires special processing An exception handler is.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
LECTURE LECTURE 14 Exception Handling Textbook p
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception Handling How to handle the runtime errors.
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.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Exceptions Error Handling and Recovery
Exceptions.
Andy Wang Object Oriented Programming in C++ COP 3330
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Object Oriented Programming COP3330 / CGS5409
Exceptions with Functions
Andy Wang Object Oriented Programming in C++ COP 3330
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 and Reading / Writing Files
Exceptions CSCE 121 J. Michael Moore
Exception Handling.
Exceptions 1 CMSC 202.
Object-Oriented Programming (OOP) Lecture No. 43
CMSC 202 Exceptions.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

Exception Handling Fall 2008 Dr. David A. Gaitros

Exception Handling “A well behaved program never abnormally terminates. All errors or problems should be handled by the program” Exception: A problem that occurs that is outside the normal expected behavior of the system. Example: Divide by zero

Exception Handling Most exceptions are infrequent or should not happen at all. The code for the exception should be separated from the main body to avoid confusing maintenance activities. Harkins back to the day of the “ go to “ when the execution path of computer programs was less well behaved.

Exception Handling Exception handling best used for situations that would normally cause the program to abnormally terminate or produce unpredictable results: – Divide by Zero – Array subscript out of bounds – Register or number overflow or underflow – Stack overflow or underflow – Abnormal operation – Parity error

Simple Example #include using namespace std; int main() { int cookies, people; double cpp; try { cout << "Enter number of people: "; cin >> people; cout << "Enter number of cookies: "; cin >> cookies; if (cookies == 0) throw people; else if (cookies < 0) throw static_cast (people); cpp =okies/static_cast (people); cout << cookies << " cookies.\n" << people << " people.\n" << "You have " << cpp << " cookies per person.\n"; }

catch(int e) { cout << e << " people, and no cookies!\nGo buy some cookies!\n"; } catch(double t) { cout << "Second catch block type double -- do we reach it?\n"; } cout << "End of program.\n"; return 0; } }

try Block The basic handling of an exception consists of the try- throw-catch trio. Basic try block try { // Some code goes here // Possibly throw an exception // More regular code can go here. }

try Block The try block is a method of grouping code together that could be associated with one or more exceptions and note affecting the rest of the program. If an exception is detected, the alternative code is executed while the execution of the normal code within the try block is halted. The code after the try block is not affected. If an exception occurs within a try block, the try block ends and the program attempts to match the exception with one of the catch handlers. If match found, code in catch is executed Only one catch block will be executed Execution resumes after last catch block

Example try-catch try { if(Array_Index > 100) throw Array_Index; } catch ( int e) { cout << e<< “ is greater than 100”<< << “resetting index to 100”\n; Array_Index = 100; }

Overuse of Exceptions Exceptions alter flow of control – Similar to old "goto" construct – "Unrestricted" flow of control Should be used sparingly Good rule: – If desire a "throw": consider how to write program without throw – Ask yourself how often you expect to encounter this error – Errors caused by bad user input is usually not a reason to write an exception to handle it – If alternative reasonable  do it