More C++ Concepts Exception Handling Templates.

Slides:



Advertisements
Similar presentations
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
Advertisements

1 Chapter 17-2 Templates and Exceptions Dale/Weems.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
CS Advanced C++ Exception Handling Topic #5.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
1 Lecture 8: Introduction to C++ Templates and Exceptions  C++ Function Templates  C++ Class Templates.
Introduction to C++ Templates and Exceptions l C++ Function Templates l C++ Class Templates l Exception and Exception Handler.
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
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.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Templates.
1 Chapter 17-1 Templates and Exceptions Dale/Weems.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Copyright 2006, The Ohio State University Introduction to C++ Templates l C++ Function Templates l C++ Class Templates.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
1 Today’s Objectives  Announcements Homework #5 is due today. Since this date is so close to the end of the semester, no late assignments will be accepted.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
1 Chapter 17 Templates and Exceptions Dale/Weems.
1 Chapter 17 Templates and Exceptions Dale/Weems/Headington.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
Exception Handling How to handle the runtime errors.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Templates יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום.
Introduction to C++ Templates and Exceptions C++ Function Templates C++ Class Templates Exception and Exception Handler.
Templates 3 Templates and type parameters The basic idea templates is simple: we can make code depend on parameters, so that it can be used in different.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Constructors and Destructors
C ++ MULTIPLE CHOICE QUESTION
Exception handling.
Exceptions Error Handling and Recovery
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
Topics: Templates Exceptions
Programming with ANSI C ++
Exceptions, Templates, and the Standard Template Library (STL)
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
Generic programming – Function template
CS212: Object Oriented Analysis and Design
EXCEPTION HANDLING.
Chapter 17 Templates and Exceptions Part 2
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Advanced C++ Exception Handling
Exceptions and Templates
Constructors and Destructors
Exception Handling.
Exceptions 1 CMSC 202.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
Ninth step for Learning C++ Programming
Tenth step for Learning C++ Programming
Object-Oriented Programming (OOP) Lecture No. 44
Function Templates Class Templates
Lecture 9.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Lecture 8: Introduction to C++ Templates and Exceptions
Exceptions for safe programming.
CMSC 202 Exceptions.
Exception Handling.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

More C++ Concepts Exception Handling Templates

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

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.

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

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

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

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 ( )

Output Foo constructor Foo destructor Caught exception

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.

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

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

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

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

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;

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 ) { … }

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.

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?