Resource Management - Object Oriented Programming - Lecture Slides, Slides of Object Oriented Programming

Templates and Inheritance, In case of exception, First Attempt, Code duplication, Second Attempt, Exception in Constructors, Exception in Initialization List, Exceptions in Destructors are the points you can learn in this object oriented programming subject.

Typology: Slides

2011/2012

Uploaded on 11/09/2012

bacha
bacha 🇮🇳

4.3

(41)

213 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-Oriented Programming
(OOP)
Lecture No. 45
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Resource Management - Object Oriented Programming - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming (OOP) Lecture No. 45

Resource Management

  • Function acquiring a resource must properly release it
  • Throwing an exception can cause resource wastage

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; }

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;

}

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 EmailAddress; … }

  • If the constructor of the SecondName throws an exception then the destructor for the First Name will be called

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

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

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