Lab Sheet 9
Lab Sheet 9
Understanding the Concept of Templates and Exception
Overloaded functions normally are used to perform similar operations on different types of data. If the operations are identical for each type, it is more convenient to use function templates. The programmer writes a single function template definition. Based on the argument types provided explicitly or inferred from calls to this function, the compiler generates separate object codes of functions to handle each function call appropriately.
All function template definitions begin with a keyword template followed by a list of template parameters to the function template enclosed in angle brackets (< and >); each formal type parameter must be preceded by either of the interchangeable keywords class or typename, as in
Let's see an example.
The template concept can be extended to classes. Class templates are generally used for data storage classes. The approach is similar to that used in the function template. Here the template keyword and class name signal that the entire class will be a template.
Let's see an example.
Exception handling is designed to handle only synchronous exceptions. The mechanism provides means to detect and report an exceptional circumstance so that appropriate action can be taken. The mechanism suggests a separate error handling code that performs the following tasks.
Find the problem ( Hit the exception)
Inform that an error has occurred( Throw the exception)
Receive the error information( Catch the exception)
Take corrective actions( Handle the exception)
Exception handling is done in the following form.
Let's see the following example.
Multiple Catch Statement
A program can have more than one condition to throw an exception. It is possible to use multiple catch statements with try block. When an exception is thrown, the exception handlers are searched in order for an appropriate match. The first handler that yields a match is executed, after executing the handler; the control goes to the first statement after the last catch block for that try.
The multiple catch construct has the following form.
Re-throwing an exception
A handler may decide to re-throw the exception caught without processing it. In such a situation, we may simply invoke throw without any arguments as shown below:
This causes the current exception to be thrown to the next enclosing try/catch sequence and is caught by a catch statement listed after that enclosing try block.
Create a function called sum() that returns the sum of the elements of an array. Make this function into a template so it will work with any numerical type. Write a program that applies this function to data of various types.
Write a class template for queue class. Assume the programmer using the queue won't make mistakes, like exceeding the capacity of the queue or trying to remove an item when the queue is empty. Define several queues of different data types and insert and remove data from them.
Modify the stack class given in the previous lab to add the exception when a user tries to add an item while the stack is full and when the user tries to add the item while the stack is empty. Throw exceptions in both of the cases and handle these exceptions.
Write any program that demonstrates the use of multiple catch handling, re-throwing an exception, and catching all exceptions.