Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exceptions handling Try, catch blocks Throwing exceptions.

Similar presentations


Presentation on theme: "Exceptions handling Try, catch blocks Throwing exceptions."— Presentation transcript:

1 Exceptions handling Try, catch blocks Throwing exceptions.
Exceptions in C++ Exceptions handling Try, catch blocks Throwing exceptions.

2 What is an Exception? An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

3 Example (file not found)
void readFile() { openInput = open(“input.txt”); strings[] s = readFrom(openFile); doStuff(s); close(openFile); } Should work fine, however, what if the file input.txt is not there one day when you are running this program?

4 3 kinds of exceptions 1) Checked exceptions. User can anticipate Ex) file not found. 2) Error exceptions. Error that are external to the applications like hardware exceptions. 3) Runtime exception. Logic error in code and improper used of API, etc.

5 Class definitions for exception objects
Consider the following : The Mathematical exception library might be organized like this. class Matherr { virtual void debug_print( ) const { cerr << “Math error”;} } ; class Overflow : public Matherr { } ; class Underflow : public Matherr { } ; class Zerodevide: public Matherr { } ; class overflow : public Matherr const char* op; int a1, a2: public: void int_overflow(const char* p, int a, int b) { op = p;a1= x; a2 = y;} virtual void debug_print( ) const { cerr << op << ‘(‘ << a1 << ‘,’ << a2 << ‘)’; }

6 Catching exceptions void f( ) { try { // some math code goes here }
catch (Overflow) { // code to handle an over flow catch(Matherr) { // Handle any Matherr that is not an overflow catch(Dividezero) { // Handle any Divide by zero catch(…) { // Handle any exception not listed above Comments: the Dividezero will never run because Dividezero is a Matherr catch(…) will catch any exception object

7 Catch by reference/pointer
void f( ) { try { g( ); // g( ) throws an Int_overflow exception } catch(Matherr& m) { m.debug_print( ); } Since the exception object was caught by reference Int_overflow::debug_print is called. If caught by value, then Matherr::debug_print would have been called.

8 Throwing Exceptions (intentionally)
class DivideByZero { public: double divisor; DivideByZero(double x) { divisor = x}| ; }; … int divide(int x, int y) if(y==0) { throw DivideByZero(x); } } try { divide(12, 0); catch (DivideByZero divZero) cerr<<"Attempted to divide "<<divZero.divisor<<" by zero"; cout << “moving right along”

9 Exceptions are Thread Safe
C++ exception handling is thread safe. Throwing on one thread (including rethrow) has no impact on any throw/catch in progress on another thread.

10 Exceptions and Destructors
void readBook(istream& book) { while(book) page *pp = readBook(book); pp->readPage(); delete pp; } What if readPage throws and exception?

11 Consider this solution
void readBook(istream& book) { while(book) page *pp = readBook(book); try { pp->readPage(); } catch(…) { delete pp; throw; } delete pp; } Works but, we have to pepper our code with try catches.

12 Auto_pointers (a.k.a Smart Pointers)
template<class T> class AUTO_PTR { public: typedef T element_type; explicit AUTO_PTR(T *pVal = 0) throw() { if (pVal) {m_AutoPtr = pVal} else {m_AutoPtr = 0;} AUTO_PTR(const ATUO_PTR<T>& ptrCopy throw() { if (ptrCopy) {m_AutoPtr = ptrCopy} else {m_AutoPtr = 0;} AUTO_PTR<T>& operator=(AUTO_PTR& ptrCopy) throw() { if (ptrCopy) { m_AutoPtr = &ptrCopy} else {m_AutoPtr = 0; } return m_AutoPtr; } ~AUTO_PTR() { if (m_AutoPtr) { delete m_AutoPtr; } } T& operator*() const throw() { return *m_AutoPtr; } T& *operator->() const throw() { return m_AutoPtr; } T& *get() const throw() { return *m_AutoPtr; } Private: T *m_AutoPtr; };

13 Original code with auto_ptr
{ while(book) auto_ptr<page> *pp = readBook(book); pp->readPage(); // delete pp; }

14 Side fact about C++ delete of pointers
C++ guarantees that: delete p; // delete the object p points to Will be safe if p currently is null. That is, you will not be deleting part of the operating system, instead no action will be taken.

15 Exceptions during Constructer
MyMovies::MyMovies { popcorn_ptr = new popcorn(); movie_ptr = new movie(bigMovie); soda_ptr = new soda(); } If there is an exception thrown during the creating of the bigMovie, should this MyMovie object be destroyed? Should the entire destructor run during destruction?

16 C++ destroys constructed objects
C++ only destroys fully constructed objects. There is no way to know how much of the destructor to run for a partially constructed object. So, we would want the objects to be either fully constructed or not constructed at all

17 Consider this solution
MyMovies::MyMovies { popcorn = new popcorn(); try { movie = new movie(bigMovie); } catch(…) { delete popcorn; throw; } try { soda = new soda(); } catch(…) { delete popcorn; delete movie; throw; } } If a step doesn’t go well, undo everything done before it. This code duplicates the destructor. It might make sense to put the common code in private functions and have the constructors and destructors call them.

18 Add constant pointers to the problem
Suppose class designer wants the pointers to the objects are constant. Constant pointers can only be initialized in an initialization list. They can not be done in a constructor body.

19 Consider another solution
MyMovies::MyMovies( const popcorn& p, const movie& m, const soda& s) : popcorn_ptr(p != “” ? new popcorn() : 0); movie_ptr(m != “” ? new movie() : 0); soda_ptr(s != “” ? new soda() : 0); { } private: auto_ptr<popcorn> popcorn; auto_ptr<movie> movie_ptr; auto_ptr<soda> soda_ptr;

20 Exceptions during exception handling
If a program throws an exception and during that exception handling, another exception is thrown, what should the operating system do? Ignore the second exception and continue handling the first. Abandon the first exception handler and handle the second one. Run the second exception handler and then return to the first. Completely give up and call “terminate”.

21 Don’t throw exception from destructors
During exception handling, all objects that are completely constructed will be destructed after the code in the exception handler runs. This is part of exception handling. What if one of your many destructors throws an exception which your destructor catches? Answer: If the destructor ran as part of normal processing, your exception handler will run. If it ran during some exception handling, then it will cause “terminate” to run and therefore all other objects that need to be destroyed after yours will not be destroyed (memory leak)

22 Exception object’s types
C++ specifies that an object thrown as an exception is always copied. This copying happens even if the object type is not in danger of being deleted (static, heap, etc. ) They are copied as the static type of the receiving object.

23 throw vs. throw e ... Catch (MathError& ME) { throw; // throws exception of whatever type came in } vs. throw ME; // throws exception of type MathError

24 throw() in the exception specification
void func() // may throw any exception void gunc() throw(A,B) { } // only throws A and B void hunc() throw() { } // doesn’t throw any exception void junc() throw () // claims to throw nothing but… { func() ; // …it may throw anything }


Download ppt "Exceptions handling Try, catch blocks Throwing exceptions."

Similar presentations


Ads by Google