Object-Oriented Programming (OOP) Lecture No. 44

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Advertisements

Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
Exception Handling Introduction Try Throw Catch. Exception Handling Use exception handling to write programs that are –Clearer –More robust –More fault-tolerant.
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.
1 Chapter 17-2 Templates and Exceptions Dale/Weems.
Exception Handling Handling an unexpected behaviour Unit - 08.
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.
CSIS 123A Lecture 11 Exception Handling. Introduction  Typical approach to development:  Write programs assuming things go as planned  Get ‘core’ working.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 CSC241: Object Oriented Programming Lecture No 28.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
Introduction to C++ Templates and Exceptions l C++ Function Templates l C++ Class Templates l Exception and Exception Handler.
Exceptions COMPSCI 105 S Principles of Computer Science.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
1 CSC241: Object Oriented Programming Lecture No 27.
CSC 270 – Survey of Programming Languages C++ Lecture 6 – Exceptions.
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
Exceptions. Why exceptions? We often strive for writing portable reusable code; we are able to detect errors, however our code may be used for many different.
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 C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
Exception Handling Outline 23.1 Introduction
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
LECTURE LECTURE 14 Exception Handling Textbook p
1 Chapter 17 Templates and Exceptions Dale/Weems.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 22.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
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.
Exception handling.
Exception Handling in C++
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
16 Exception Handling.
Chapter 16 Exception Handling
Abstract Class Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for.
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
CS 2704 Object Oriented Software Design and Construction
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 10 – Exception Handling
Object-Oriented Programming (OOP) Lecture No. 45
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
Exceptions with Functions
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 17 Templates and Exceptions Part 2
Chapter 12 Exception Handling and Text IO
Throwing and catching exceptions
Advanced C++ Exception Handling
CSC 270 – Survey of Programming Languages
Exception Handling.
Exceptions 1 CMSC 202.
Object-Oriented Programming (OOP) Lecture No. 43
Tenth step for Learning C++ Programming
Lecture 9.
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
Exceptions for safe programming.
Exception Handling.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

Object-Oriented Programming (OOP) Lecture No. 44

Example class DivideByZero { public: DivideByZero() { } }; int Quotient(int a, int b){ if(b == 0){ throw DivideByZero(); return a / b;

main Function int main() { try{ … quot = Quotient(a,b); … } catch(DivideByZero) { return 0;

Stack Unwinding The flow control of throw is referred to as stack unwinding Stack unwinding is more complex than return statement Return can be used to transfer the control to the calling function only Stack unwinding can transfer the control to any function in nested function calls

Stack Unwinding All the local objects of a executing block are destroyed when an exception is thrown Dynamically allocated memory is not destroyed automatically If no catch handler catches the exception the function terminate is called, which by default calls function abort

Example void function1() { ... throw Exception(); … } int main() { try{ function2(); } catch( Exception ) { } return 0;

Function-Call Stack function1() function2() main() function2() main()

Stack Unwinding The stack unwinding is also performed if we have nested try blocks

Example int main( ) { try { throw 1; } catch( float ) { } catch( int ) { } return 0;

Stack Unwinding Firstly the catch handler with float parameter is tried This catch handler will not be executed as its parameter is of different type – no coercion Secondly the catch handler with int parameter is tried and executed

Catch Handler We can modify this to use the object to carry information about the cause of error The object thrown is copied to the object given in the handler Use the reference in the catch handler to avoid problem caused by shallow copy

Example class DivideByZero { int numerator; public: DivideByZero(int i) { numerator = i; } void Print() const{ cout << endl << numerator << “ was divided by zero”; };

Example int Quotient(int a, int b) { if(b == 0){ throw DivideByZero(a); } return a / b;

Body of main Function for ( int i = 0; i < 10; i++ ) { try { GetNumbers(a, b); quot = Quotient(a, b); ... } catch(DivideByZero & obj) { obj.Print(); i--; } cout << “\nSum of ten quotients is ” << sum;

Output Enter two integers 10 Quotient of 10 and 10 is 1 10 was divided by zero ... // there will be sum of exactly ten quotients

Catch Handler The object thrown as exception is destroyed when the execution of the catch handler completes

Avoiding too many Catch Handlers There are two ways to catch more then one object in a single catch handler Use inheritance Catch every exception

Inheritance of Exceptions MathError DivideByZero IntergerOutOfRange

Grouping Exceptions try{ ... } catch(DivideByZero){ catch(IntergerOutOfRange){ catch (InputStreamError){

Example–With Inheritance try{ ... } catch (MathError){ catch (InputStreamError){

Catch Every Exception C++ provides a special syntax that allows to catch every object thrown catch ( ... ) { //... }

Re-Throw A function can catch an exception and perform partial handling Re-throw is a mechanism of throw the exception again after partial handling throw; /*without any expression*/

Example int main ( ) { try { Function(); } catch(Exception&) { ... return 0;

Example void Function( ) { try { /*Code that might throw an Exception*/ } catch(Exception&){ if( can_handle_completely ) { // handle exception } else { // partially handle exception throw; //re-throw exception } } }

Order of Handlers Order of the more then one catch handlers can cause logical errors when using inheritance or catch all

Example try{ ... } catch (...) { ... catch ( MathError ) { ... catch ( DivideByZero ) { ... // last two handler can never be invoked