Final Lecture
Final Lecture
Exceptions
Templates
Object-oriented Systems Analysis And Design Using Uml
EXCEPTIONS
• Exceptions are run time anomalies or unusual conditions that a
program may encounter during execution.
• Conditions such as
• Division by zero
• Access to an array outside of its bounds
• Running out of memory
• Running out of disk space
• It was not a part of original C++.
• It is a new feature added to ANSI C++.
EXCEPTION HANDLING
• Exceptions are of 2 kinds
• Synchronous Exception:
• Out of rage
• Over flow
• Asynchronous Exception: Error that are caused by causes beyond the control
of the program
• Keyboard interrupts
catch block
}
• catch(type arg)
{
…
…
…
}
EXCEPTION HANDLING MECHANISM (CONT…)
• If the type of the object thrown matches the arg type in the catch
statement, the catch block is executed.
• More than one catch statement can be associated with a try block.
CATCHING MECHANISM (CONT…)
try
{
throw exception;
}
catch(type1 arg)
{
// catch block 1
}
catch(type2 arg)
{
// catch block 2
}
…
…
catch(typeN arg)
{
// catch block N
}
CATCHING MECHANISM (CONT…)
• When an exception is thrown, the exception handlers are searched in
order for a match.
• The first handler that yields a match is executed.
• If several catch statement matches the type of an exception the first
handler that matches the exception type is executed.
• Catch all exception
catch (…)
{
// statement for processing all exceptions
}
RETHROWING AN EXCEPTION
• A handler may decide to rethrow the exception caught without processing it.
• In such a case we have to invoke throw without any arguments as shown below
throw;
• This causes the current exception to be thrown to the next enclosing try/catch
sequence and is caught by a catch statement listed after the enclosing try
block
Templates
• Function Templates
• Syntax, defining
• Compiler complications
• Class Templates
• Syntax
• Example: array template class
• Templates and Inheritance
• Example: partially-filled array template class
template<class T>
void showStuff(int stuff1, T stuff2, T stuff3);
• Definition:
template<class T>
void showStuff(int stuff1, T stuff2, T stuff3)
{
cout << stuff1 << endl
<< stuff2 << endl
<< stuff3 << endl;
}
template<class T>
void Pair<T>::setFirst(T newVal)
{
first = newVal;
}