CSC241 Object-Oriented Programming (OOP) Lecture No. 22.

Slides:



Advertisements
Similar presentations
L7: Exceptions Problem Description Error Handling –terminate the program –return a value representing an error –return a legal value and leave the program.
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Exception Handling Handling an unexpected behaviour Unit - 08.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
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.
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.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Monday, Mar 24, 2003Kate Gregory with material from Deitel and Deitel Week 11 Exceptions.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
C++ Exception Handling
1 CSC241: Object Oriented Programming Lecture No 28.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
CS Advanced C++ Exception Handling Topic #5.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
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.
. Plab – Tirgul 10 Exceptions. Error handling in C Up to now we handled our errors in the “C way”: assert return codes global error variable ( errno and.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
Plab – Tirgul 8 Exceptions. Error handling in C Up to now we handled our errors in the “C way”: assert return codes global error variable ( errno and.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
SNU OOPSLA Lab. 14. Exception Handling © copyright 2001 SNU OOPSLA Lab.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
Object Oriented Programming
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.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
Object Oriented Programming Elhanan Borenstein Lecture #4.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exception Handling Programmers must deal with errors and exceptional situations: User input errors Device errors Empty disk space, no memory Component.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 23 - Exception Handling Outline 23.1Introduction.
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 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.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
Object-Oriented Programming (OOP) Lecture No. 24.
Chapter 2 Objects and Classes
Exception handling.
Exception Handling in C++
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
CS212: Object Oriented Analysis and Design
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
Object-Oriented Programming (OOP) Lecture No. 45
Part IX Fundamentals of C and C++ Programming Exception Handling
Chapter 14: Exception Handling
Object-Oriented Programming (OOP) Lecture No. 43
Object-Oriented Programming (OOP) Lecture No. 44
Java Basics Exception Handling.
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.
Presentation transcript:

CSC241 Object-Oriented Programming (OOP) Lecture No. 22

Resource Management  Function acquiring a resource must properly release it  Throwing an exception can cause resource wastage

Example int function1(){ FILE *fileptr = fopen("filename.txt", "w");... throw exception();... fclose(fileptr); return 0; }

Resource Management  In case of exception the call to close will be ignored

First Attempt int function1(){ try{ FILE *fileptr = fopen("filename.txt", "w"); fwrite("Hello World", 1, 11, fileptr);... throw exception(); fclose(fileptr); } catch (...) { fclose(fileptr); throw; } return 0; }

Resource Management  There is code duplication

Second Attempt class FilePtr{ FILE * f; public: FilePtr(const char *name, const char * mode){ f = fopen(name, mode); } ~FilePtr(){ fclose(f); } operator FILE * (){ return f; } };

Example int function1(){ FilePtr file("filename.txt", "w"); fwrite("Hello World", 1, 11, file); throw exception();... return 0; }

Resource Management  The destructor of the FilePtr class will close the file  Programmer does not have to close the file explicitly in case of error as well as in normal case

Exception in Constructors  Exception thrown in constructor cause the destructor to be called for any object built as part of object being constructed before exception is thrown  Destructor for partially constructed object is not called

Example class Student{ String FirstName; String SecondName; String Address; … } If the constructor of the SecondName throws an exception then the destructor for the First Name will be called

Exception in Initialization List  Exception due to constructor of any contained object or the constructor of a parent class can be caught in the member initialization list

Example Student::Student(String aName) : name(aName) /*The constructor of String can throw a exception*/ {... }

Exception in Initialization List  The programmer may want to catch the exception and perform some action to rectify the problem

Example Student::Student(String aName) try : name(aName) {... } catch (…) { }

Exceptions in Destructors  Exception should not leave the destructor  If a destructor is called due to stack unwinding, and an exception leaves the destructor then the function std::terminate() is called, which by default calls the std::abort()

Example class Exception; class Complex{ … public: ~Complex(){ throw Exception(); } };

Example int main(){ try{ Complex obj; throw Exception(); … } catch (…){ } return 0; } // The program will terminate abnormally

Example Complex::~Complex() { try{ throw Exception(); } catch (…){ }

Exception Specification  Program can specify the list of exceptions a function is allowed to throw  This list is also called throw list  If we write empty list then the function wont be able to throw any exception

Syntax void Function1() { … } void Function2() throw () { … } void Function3() throw (Exception1, …){}  Function1 can throw any exception  Function2 cannot throw any Exception  Function3 can throw any exception of type Exception1 or any class derived from it

Exception Specification  If a function throws exception other then specified in the throw list then the function unexpected is called  The function unexpected calls the function terminate

Exception Specification  If programmer wants to handle such cases then he must provide a handler function  To tell the compiler to call a function use set_unexpected