Title: ECE 538 Object Oriented Concepts Session 11
1ECE 538 Object Oriented ConceptsSession 11
- Dr. John G. Weber
- John.Weber_at_notes.udayton.edu
- http//academic.udayton.edu/JohnWeber
2Templates and Exceptions
- Templates
- Use one function or class to handle different
data types - Exceptions
- Uniform way to handle errors in classes
3UML and Templates
Dependency
Stereotype
4Definitions
- Dependency
- Relationship between two elements such that a
change in the independent one may cause a change
in the dependent one. - In previous chart, template class is independent
element and s1 and s2 are dependent elements - Stereotype
- Way of specifying additional detail about UML
element. - Contained in double angle brackets
- Use in the previous chart indicates that the
template class instantiates the dependent element
(specific class) using specified parametersshown
in parentheses after the stereotype
5Exceptions
- Provide a systematic, object-oriented approach to
handling errors generated by C classes. - Exceptions are errors that occur at run-time
- Examples out of memory, cant open a file,
incorrect object init, etc. - Why use?
- Results from complexity introduced by class
concept
6C- type errors
- C programs oftern signal error by returning a
particular value from the function where the
error occurred. - Implies that calling code must check for each
error code that could be returned. - Every call to this function must be examined by
calling program
7Class errors
- When classes are used, a function may not be
explicitly called when errors are introduced - e.g. Define objects of a particular class
- SomeClass obj1, obj2, obj3
- if constructor has error, how do we find?
- Problem worse when class libraries are used
- User of the library may not be the developer
- Problem of communicating errors from within class
member functions to user of lib
8Exception Syntax
Code using objects of the class is enclosed in
Try block. Errors generated by code in try
block will be caught in catch block.
Install separate block of code called exception
handler or catchblock to catch exceptions
When member functions detect errorthey throw
exceptions
9New Key Words
- Throw
- Used to generate or throw and exception
- Try
- Identifies block of code that operates on objects
that might cause an exception - Catch
- Identifies block of code that handles the
exceptions for a particular class
10Example
// xstak.cpp // demonstrates exceptions include
using namespace std const int MAX
3 //stack holds 3
ints /////////////////////////////////////////////
/////////////////// class Stack private
int stMAX //stack array of
integers int top //index
of top of stack public class Range
//exception class for Stack
//note empty class body
//-------------------------------------------
------------------- Stack()
//constructor top -1
//----------------------------------------------
---------------- void push(int var)
if(top MAX-1) //if stack full,
throw Range() //throw exception
sttop var //put number on
stack //----------------------------------------
---------------------- int pop()
if(top
throw Range() //throw exception
return sttop-- //take number off stack
11Example(cont)
int main() Stack s1 try
s1.push(11) s1.push(22)
s1.push(33) // s1.push(44)
//oops stack full cout s1.pop() endl cout //oops stack empty catch(StackRange)
//exception handler
cout endl cout (or normal exit)"
12Output
13Features of Example Dealing with Exceptions
- Specifying the Exception Class
- class Range
- //noteempty class body
- Objects of this class have no data and no member
functions - Data and member functions may be used in
exception classes - Class name is used to connect a throw statement
with a catch block - Throwing an Exception
- Exception occurs if application tries to pop a
value when stack is empty or push a value when
stack is full - Member functions check for these errors and throw
exception if they occur - In this example, same exception is thrown in two
places (one for push error and one for pop error)
14Features of Example Dealing with Exceptions (cont)
- Try block
- All statements in main() that might cause this
exception are enclosed in braces and preceded by
try keyword - Part of normal code
- May have multiple try blocks
- Not all code must be in try blocks, just that
which might cause exceptions - Exception Handler (catch block)
- Code that handles exceptions is enclosed in
braces preceded by catch keyword - Must immediately follow try block
- Control may fall through bottom of exception
handler so processing continues at that point. - Exception handler may also transfer control
elsewhere or terminate the program
15Review Exceptions
- Sequence
- Code is executing normally outside a try block
- Control enters try block
- Statement in try block causes error in member
function - Member function throws exception
- Control transfers to catch block following try
block - Advantages
- Code is clean
- Any statement in try block could cause an
exception - Dont need to worry about checking for each one
16Multiple Exceptions
- Class may through as many exceptions as we need
- Define multiple exception classes
- Insert appropriate throw statements
- Use multiple catch blocks
17Example
// xstak2.cpp // demonstrates two exception
handlers include using namespace
std const int MAX 3 //stack
holds 3 ints /////////////////////////////////////
/////////////////////////// class Stack
private int stMAX //stack
array of integers int top
//index of top of stack public class
Full //first exception class
class Empty //second exception
class //------------------------------------------
-------------------- Stack()
//constructor top -1
//----------------------------------------------
---------------- void push(int var)
//put number on stack if(top
MAX-1) //if stack full, throw
Full() //throw Full exception
sttop var //--------------------
------------------------------------------
int pop() //take number off
stack if(top //if stack empty, throw Empty()
//throw Empty exception return
sttop--
18Example (cont)
int main() Stack s1 try
s1.push(11) s1.push(22)
s1.push(33) // s1.push(44)
//oops stack full cout s1.pop() endl cout //oops stack empty catch(StackFull)
cout endl catch(StackEmpty)
cout return 0
19Catch Blocks for Multiple Exceptions
- Note that we had one try block but multiple catch
blocks - All catch blocks used with a particular try block
must follow immediately after the try block - Only one catch block is activated for a given
exception - Multiple catch blocks operate similar to a switch
statement (with breaks) - Control goes to end of catch ladder when
exception is handled
20Exceptions with Arguments
- Use when application needs more information about
exception causes. - Modify stack exception routine to tell us which
member function created the error and what value
did not get pushed on the stack.
21Example
// xstak3.cpp // demonstrates two exception
handlers include include
using namespace std const int MAX 3
//stack holds 3 ints /////////////////
/////////////////////////////////////////////// cl
ass Stack private int stMAX
//stack array of integers int
top //index of top of stack
public class Full //exception
class public string origin //name of
routine throwing exception int invar //value
of input variable Full(string or, int
in) //2-arg constructor origin
or invar in class Empty
//exception class
22Example (Cont)
//------------------------------------------------
-------------- Stack()
//constructor top -1
//----------------------------------------------
---------------- void push(int var)
//put number on stack if(top
MAX-1) //if stack full, throw
Full("push operation", var) //throw Full
exception with attributes sttop
var //--------------------------------
------------------------------ int pop()
//take number off stack
if(top throw Empty() //throw Empty
exception return sttop--
23Example (Cont)
int main() Stack s1 try
s1.push(11) s1.push(22)
s1.push(33) s1.push(44)
//oops stack full cout s1.pop() endl cout //oops stack empty catch(StackFull
inx) cout catch(StackEmpty) cout "Exception Stack Empty" return 0
24Function Nesting
- Statement causing exception need not be located
directly in the try block - May be in a function called by a function in the
try block - Implies that only the top level of the program
need have a try block - Lower level functions will be covered if they are
called directly or indirectly by a function in
the try block - It may be useful for intermediate level functions
to catch a lower level exception, add data, and
then throw the exception to the upper level.
25Exceptions and Class Libraries
- Exceptions solve the problem of errors in class
libraries - Library routine may find error but doesnt know
what to do about it - Exceptions allow library routine to pass error to
calling routine so the calling routine can decide
what to do. - When developing a class library, cause it to
throw exceptions for anything that could cause
problems - When using a class library, provide try catch
blocks to catch the exceptions
26Thoughts on Exceptions
- Where to use
- Not everywhere
- Exceptions result in overhead (size and time)
- Dont use for anything easily checked by program
- e.g. user input
- Destructors Called Automatically
- When exception thrown, destructor called for any
object created by the code up to that point in
the try block - Guarantees that code within try block will have
been reset with respect to objects - Handling Exceptions
- Optionsterminate program, try to handle the
problem and recover - If you dont catch the exceptions, OS will
terminate the program