Presentation is loading. Please wait.

Presentation is loading. Please wait.

More C++ Concepts Exception Handling Templates.

Similar presentations


Presentation on theme: "More C++ Concepts Exception Handling Templates."— Presentation transcript:

1 More C++ Concepts Exception Handling Templates

2 Exceptions An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring at runtime When an exception occurs Do not do anything Cryptic error messages Sytem crash Handle the error Issue a warning and exit Handle exception gracefully and continue Examples: Divide by zero, Out of memory

3 Where to handle Can we handle in the same section of code where exception is raised? Higher level code will have better idea as how to handle Different programs using same classes will handle differently Separation of creation and handling of exception Pass exceptions to calling functions Need a mechanism for Need to distinguish the code which can raise exceptions Bundle and send the information to caller Methods to operate on the information passed This separation of exception creation and exception handling is very significant. Higher level sections of code can better handle exceptions in many cases. Suppose an exception occurs in a library routine. That routine cannot know how to respond in a way that is appropriate for your program. In some cases the appropriate response might be to terminate the program. In other cases, the appropriate response might be a warning message. In others, maybe the exception can be caught and disregarded. The method in which an exception occurs could just alert its caller. This allows code that raises exceptions to be developed separately from code that handles them.

4 Handling exceptions In C++, exception is an object (simple or user-defined) A way to convey information to caller Code that detects the abnormality throws the exception try block – code that can raise (throw) exception Code that handles catches the exception catch block – code that can handle (catch) the exception try { throw <object> } catch (<type> <objname>) { cout << "Exception"; Exception object, say i Object, say int Name, say e

5 Example Bundle information Must agree on datatype
int main() { int x = 5; int y = 0; int result; int exceptionCode = 25; try { if (y == 0) { throw exceptionCode; } result = x/y; catch (int e) { if (e == 25) { cout << "Divide by zero" << endl; else { cout << "Exception of unknown type" << endl; cout << "Goodbye" << endl; return 0; Must agree on datatype Bundle information throw “Divide by zero”; catch ( string e ) { cout << e << endl; }

6 If not, pass the exception to caller
If the exception thrown is of type specified for a handler, then handler is excuted If not, pass the exception to caller If no appropriate catching block, program terminates main () { try { foo (); } catch (int e) { printf("from main - %d\n", e); void foo ( void ) { try { int i = 10; throw i; } catch (string s) { cout << s ; Output: from main - 10

7 Stack Unwinding Call Stack main ( ) f ( ) g ( ) g ( ) f ( ) main ( )
class Foo { public: Foo() {cout << "Foo constructor" << endl;} ~Foo() {cout << "Foo destructor" << endl;} }; main() void f(); try // Turn on exception handling f(); } catch(int) cout << "Caught exception" << endl; return 0; void f() { void g(); Foo x; g(); } void g() throw 1; main ( ) f ( ) g ( ) Call Stack main ( ) f ( ) g ( )

8 Output Foo constructor Foo destructor Caught exception

9 Templates Provides support for generic programming
Focus on algorithms and DS rather than on data types Data types are parameters Helps in developing generic & flexible behavior Function Templates Class Templates Code for all types is centralized Easy maintenance Better re-usage of code A program may require a queue of customers and a queue of messages. One could easily implement a queue of customers, then take the existing code and implement a queue of messages. The program grows, and now there is a need for a queue of orders. So just take the queue of messages and convert that to a queue of orders (Copy, paste, find, replace????). Need to make some changes to the queue implementation? Not a very easy task, since the code has been duplicated in many places.

10 Function Templates Generic functions that can be used for arbitrary types Perform identical operations on different types Approaches Naïve approach Function overloading Function templates void PrintInt( int n ) { cout << "***Debug" << endl; cout << "Value is " << n << endl; } void PrintChar( char ch ) cout << "Value is " << ch << endl; void PrintFloat( float x ) { … } void PrintDouble( double d ) Naïve Approach PrintInt (sum); PrintChar (c); PrintFloat (angle);

11 Function Overloading Print (sum); Print (c); Print (angle);
void Print( int n ) { cout << "***Debug" << endl; cout << "Value is " << n << endl; } void Print( char ch ) cout << "Value is " << ch << endl; void Print( float f ) ... void Print( double d ) Print (sum); Print (c); Print (angle);

12 Function Templates Compiler generates multiple versions of a function based on parameterized data types FunctionTemplate Template < TemplateParamList > FunctionDefinition Template Parameters template <class T> void Print( T val ) { cout << "***Debug" << endl; cout << "Value is " << val << endl; } Print<int> (sum); Print<char> (initial); Print<float> (angle); Template Arguments

13 Naïve Approach Function Overloading Template Functions
Different Function Definitions Different Function Names Function Overloading Different Function Definitions Same Function Name Template Functions One Function Definition (a function template) Compiler Generates Individual Functions

14 Class Templates Definition of generic classes with parameterized types
Template < TemplateParamList > ClassDefinition template <class T> class Stack { public: Stack(int n = 10) { stackPtr = new T[n]; } ~Stack() { delete [] stackPtr ; } int push(const T&); int pop(T&) ; // pop an element off the stack private: int size ; // Number of elements on Stack int top ; T* stackPtr ; }; Stack<int> iS; Stack<float> fS; Stack<char> cS; typedef Stack<int> intStk; typedef Stack<char> charStk; intStk iS; charStk cS;

15 For each set of template arguments, compiler creates a separate class
Buffer<char, 128> cbuf; Buffer<int, 100> ibuf; Buffer<Record, 8> rbuf; template <class T, int max > class Buffer { public: Buffer ( ) { … } void add ( T item ); private: T buf [ max ]; }; Record is a class For each set of template arguments, compiler creates a separate class Process of generation of each class is Instantiation Each new class is Specialization template <class T, int max> void Buffer<T, max>::add ( T item ) { }

16 Template Instantiation
Z<int> iz; // implicit instantiation of class Z<int> Z<float> fz; iz.g(); // generates function Z<int>::g( ) but not f( ) fz.f(); // generates function Z<float>::f( ) but not g( ) template Z<int>; // explicit instantiation of class Z<int> Z<int> *pi; // instantiation is NOT required template <class T> class Z { public: Z() { … } ; ~Z() { … } ; void f ( ) { … } ; void g ( ) { … }; }; template <class T> T max ( T a, T b ) { return a > b ? a : b ; } int i = max ( 10, 20 ); // implicit: int max(int, int) char c = max ( ‘s’, ‘k’ ); template int max<int>(int); // explicit: int max(int, int) Compiler will not instantiate new classes and methods unless it is required. It is an error to instantiate the template which is not defined.

17 Type Equivalence Each instantiation creates new type
Are Stack<int> and Stack<float> have same type? typedef unsigned int Uint; Are Stack<unsigned int> and Stack<Uint> have same type?

18


Download ppt "More C++ Concepts Exception Handling Templates."

Similar presentations


Ads by Google