Module12 Exceptions, Templates, and Other Advanced Topics

An exception is an error that occurs at runtime. Using C++’s exception handling subsystem, you can, in a structured and controlled manner, handle runtime errors. When exception handling is employed, your program automatically invokes an error-handling routine when an exception occurs. The principal advantage of exception handling is that it automates much of the error-handling code that previously had to be entered “by hand” into any large program.

Module12
Exceptions, Templates, and Other
Advanced Topics
Table of Contents
CRITICAL SKILL 12.1: Exception Handling ...................................................................................................... 2
CRITICAL SKILL 12.2: Generic Functions ...................................................................................................... 14
CRITICAL SKILL 12.3: Generic Classes .......................................................................................................... 19
CRITICAL SKILL 12.4: Dynamic Allocation .................................................................................................... 26
CRITICAL SKILL 12.5: Namespaces............................................................................................................... 35
CRITICAL SKILL 12.6: static Class Members ................................................................................................. 42
CRITICAL SKILL 12.7: Runtime Type Identification (RTTI) ............................................................................ 46
CRITICAL SKILL 12.8: The Casting Operators ............................................................................................... 49
You have come a long way since the start of this book. In this, the final module, you will examine several
important, advanced C++ topics, including exception handling, templates, dynamic allocation, and
namespaces. Runtime type ID and the casting operators are also covered. Keep in mind that C++ is a
large, sophisticated, professional programming language, and it is not possible to cover every advanced
feature, specialized technique, or programming nuance in this beginner’s guide. When you finish this
module, however, you will have mastered the core elements of the language and will be able to begin
writing real-world programs.
1 C++ A Beginner’s Guide by Herbert Schildt

CRITICAL SKILL 12.1: Exception Handling
An exception is an error that occurs at runtime. Using C++’s exception handling subsystem, you can, in a
structured and controlled manner, handle runtime errors. When exception handling is employed, your
program automatically invokes an error-handling routine when an exception occurs. The principal
advantage of exception handling is that it automates much of the error-handling code that previously
had to be entered “by hand” into any large program.
Exception Handling Fundamentals
C++ exception handling is built upon three keywords: try, catch, and throw. In the most general terms,
program statements that you want to monitor for exceptions are contained in a try block. If an
exception (that is, an error) occurs within the try block, it is thrown (using throw). The exception is
caught, using catch, and processed. The following discussion elaborates upon this general description.
Code that you want to monitor for exceptions must have been executed from within a try block. (A
function called from within a try block is also monitored.) Exceptions that can be thrown by the
monitored code are caught by a catch statement that immediately follows the try statement in which
the exception was thrown. The general forms of try and catch are shown here:
The try block must contain the portion of your program that you want to monitor for errors. This section
can be as short as a few statements within one function, or as all-encompassing as a try block that
encloses the main( ) function code (which would, in effect, cause the entire program to be monitored).
When an exception is thrown, it is caught by its corresponding catch statement, which then processes
the exception. There can be more than one catch statement associated with a try. The type of the
exception determines which catch statement is used. That is, if the data type specified by a catch
statement matches that of the exception, then that catch statement is executed (and all others are
bypassed). When an exception is caught, arg will receive its value. Any type of data can be caught,
including classes that you create.
2 C++ A Beginner’s Guide by Herbert Schildt

The general form of the throw statement is shown here:
throw exception;
throw generates the exception specified by exception. If this exception is to be caught,
Exceptions, Templates, and Other Advanced Topics
then throw must be executed either from within a try block itself, or from any function called from
within the try block (directly or indirectly).
If an exception is thrown for which there is no applicable catch statement, an abnormal program
termination will occur. That is, your program will stop abruptly in an uncontrolled manner. Thus, you will
want to catch all exceptions that will be thrown.
Here is a simple example that shows how C++ exception handling operates:
This program displays the following output:
start Inside
try block
Caught an exception -- value is: 99
end
Look carefully at this program. As you can see, there is a try block containing three statements and a
catch(int i) statement that processes an integer exception. Within the try block, only two of the three
statements will execute: the first cout statement and the throw. Once an exception has been thrown,
control passes to the catch expression, and the try block is terminated. That is, catch is not called.
3 C++ A Beginner’s Guide by Herbert Schildt

Rather, program execution is transferred to it. (The program’s stack is automatically reset, as necessary,
to accomplish this.) Thus, the cout statement following the throw will never execute.
Usually, the code within a catch statement attempts to remedy an error by taking appropriate action. If
the error can be fixed, then execution will continue with the statements following the catch. Otherwise,
program execution should be terminated in a controlled manner.
As mentioned earlier, the type of the exception must match the type specified in a catch statement. For
example, in the preceding program, if you change the type in the catch statement to double, then the
exception will not be caught and abnormal termination will occur. This change is shown here:
This program produces the following output because the integer exception will not be caught by the
catch(double i) statement. Of course, the final message indicating abnormal termination will vary from
compiler to compiler.
start Inside
try block
Abnormal program termination
An exception thrown by a function called from within a try block can be handled by that try block. For
example, this is a valid program:
4 C++ A Beginner’s Guide by Herbert Schildt

This program produces the following output:
As the output confirms, the exception thrown in Xtest( ) was caught by the exception handler in main( ).
A try block can be localized to a function. When this is the case, each time the function is entered, the
exception handling relative to that function is reset. Examine this sample program:
5 C++ A Beginner’s Guide by Herbert Schildt

This program displays the following output:
start
6 C++ A Beginner’s Guide by Herbert Schildt

Caught One! Ex. #: 1
Caught One! Ex. #: 2
Caught One! Ex. #: 3
end
In this example, three exceptions are thrown. After each exception, the function returns. When the
function is called again, the exception handling is reset. In general, a try block is reset each time it is
entered. Thus, a try block that is part of a loop will be reset each time the loop repeats.
1. In the language of C++, what is an exception?
2. Exception handling is based on what three keywords?
3. An exception is caught based on its type. True or false?
Using Multiple catch Statements
As stated earlier, you can associate more than one catch statement with a try. In fact, it is common to do
so. However, each catch must catch a different type of exception. For example, the program shown next
catches both integers and character pointers.
7 C++ A Beginner’s Guide by Herbert Schildt

In general, catch expressions are checked in the order in which they occur in a program. Only a matching
statement is executed. All other catch blocks are ignored.
Catching Base Class Exceptions
There is one important point about multiple catch statements that relates to derived classes. A catch
clause for a base class will also match any class derived from that base. Thus, if you want to catch
exceptions of both a base class type and a derived class type, put the derived class first in the catch
sequence. If you don’t, the base class catch will also catch all derived classes. For example, consider the
following program:
8 C++ A Beginner’s Guide by Herbert Schildt

Here, because derived is an object that has B as a base class, it will be caught by the first catch clause,
and the second clause will never execute. Some compilers will flag this condition with a warning
message. Others may issue an error message and stop compilation. Either way, to fix this condition,
reverse the order of the catch clauses.
Catching All Exceptions
In some circumstances, you will want an exception handler to catch all exceptions instead of just a
certain type. To do this, use this form of catch:
catch(...) { // process all exceptions }
Here, the ellipsis matches any type of data. The following program illustrates catch(...):
9 C++ A Beginner’s Guide by Herbert Schildt

This program displays the following output:
start
Caught One!
Caught One!
Caught One!
end
Xhandler( ) throws three types of exceptions: int, char, and double. All are caught using the catch(...)
statement.
One very good use for catch(...) is as the last catch of a cluster of catches. In this capacity, it provides a
useful default or “catch all” statement. Using catch(...) as a default is a good way to catch all exceptions
that you don’t want to handle explicitly. Also, by catching all exceptions, you prevent an unhandled
exception from causing an abnormal program termination.
Specifying Exceptions Thrown by a Function
You can specify the type of exceptions that a function can throw outside of itself. In fact, you can also
prevent a function from throwing any exceptions whatsoever. To accomplish these restrictions, you
must add a throw clause to a function definition. The general form of this clause is
10 C++ A Beginner’s Guide by Herbert Schildt

ret-type func-name(arg-list) throw(type-list) { // ... }
Here, only those data types contained in the comma-separated type-list can be thrown by the function.
Throwing any other type of expression will cause abnormal program termination. If you don’t want a
function to be able to throw any exceptions, then use an empty list.
NOTE: At the time of this writing, Visual C++ does not actually prevent a function from throwing an exception
type that is not specified in the throw clause. This is nonstandard behavior. You can still specify a throw clause, but
such a clause is informational only.
The following program shows how to specify the types of exceptions that can be thrown from a
function:
11 C++ A Beginner’s Guide by Herbert Schildt

In this program, the function Xhandler( ) can only throw integer, character, and double exceptions. If it
attempts to throw any other type of exception, then an abnormal program termination will occur. To
see an example of this, remove int from the list and retry the program. An error will result. (As
mentioned, currently Visual C++ does not restrict the exceptions that a function can throw.)
It is important to understand that a function can only be restricted in what types of exceptions it throws
back to the try block that has called it. That is, a try block within a function can throw any type of
exception, as long as the exception is caught within that function. The restriction applies only when
throwing an exception outside of the function.
Rethrowing an Exception
You can rethrow an exception from within an exception handler by calling throw by itself, with no
exception. This causes the current exception to be passed on to an outer try/catch sequence. The most
likely reason for calling throw this way is to allow multiple handlers access to the exception. For
example, perhaps one exception handler manages one aspect of an exception, and a second handler
copes with another aspect. An exception can only be rethrown from within a catch block (or from any
function called from within that block). When you rethrow an exception, it will not be recaught by the
same catch statement. It will propagate to the next catch statement. The following program illustrates
rethrowing an exception. It rethrows a char * exception.
12 C++ A Beginner’s Guide by Herbert Schildt

This program displays the following output:
start
Caught char * inside Xhandler
Caught char * inside main
End
1. Show how to catch all exceptions.
2. How do you specify the type of exceptions that can be thrown out of a function?
3. How do you rethrow an exception?
Templates
The template is one of C++’s most sophisticated and high-powered features. Although not part of the
original specification for C++, it was added several years ago and is supported by all modern C++
compilers. Templates help you achieve one of the most elusive goals in programming: the creation of
reusable code.
13 C++ A Beginner’s Guide by Herbert Schildt

Using templates, it is possible to create generic functions and classes. In a generic function or class, the
type of data upon which the function or class operates is specified as a parameter. Thus, you can use
one function or class with several different types of data without having to explicitly recode specific
versions for each data type. Both generic functions and generic classes are introduced here.
Ask the Expert
Q: It seems that there are two ways for a function to report an error: to throw an exception or to
return an error code. In general, when should I use each approach?
A: You are correct, there are two general approaches to reporting errors: throwing exceptions and
returning error codes. Today, language experts favor exceptions rather than error codes. For example,
both the Java and C# languages rely heavily on exceptions, using them to report most types of common
errors, such as an error opening a file or an arithmetic overflow. Because C++ is derived from C, it uses a
blend of error codes and exceptions to report errors. Thus, many error conditions that relate to C++
library functions are reported using error return codes. However, in new code that you write, you should
consider using exceptions to report errors. It is the way modern code is being written.
CRITICAL SKILL 12.2: Generic Functions
A generic function defines a general set of operations that will be applied to various types of data. The
type of data that the function will operate upon is passed to it as a parameter. Through a generic
function, a single general procedure can be applied to a wide range of data. As you probably know,
many algorithms are logically the same no matter what type of data is being operated upon. For
example, the Quicksort sorting algorithm is the same whether it is applied to an array of integers or an
array of floats. It is just that the type of data being sorted is different. By creating a generic function, you
can define the nature of the algorithm, independent of any data. Once you have done this, the compiler
will automatically generate the correct code for the type of data that is actually used when you execute
the function. In essence, when you create a generic function, you are creating a function that can
automatically overload itself.
A generic function is created using the keyword template. The normal meaning of the word “template”
accurately reflects its use in C++. It is used to create a template (or framework) that describes what a
function will do, leaving it to the compiler to fill in the details as needed. The general form of a generic
function definition is shown here:
template ret-type func-name(parameter list) { // body of function }
Here, Ttype is a placeholder name for a data type. This name is then used within the function definition
to declare the type of data upon which the function operates. The compiler will automatically replace
Ttype with an actual data type when it creates a specific version of the function. Although the use of the
keyword class to specify a generic type in a template declaration is traditional, you may also use the
keyword typename.
14 C++ A Beginner’s Guide by Herbert Schildt

The following example creates a generic function that swaps the values of the two variables with which
it is called. Because the process of exchanging two values is independent of the type of the variables, it
is a good candidate for being made into a generic function.
Let’s look closely at this program. The line
template void swapargs(X &a, X &b)
tells the compiler two things: that a template is being created and that a generic definition is beginning.
Here, X is a generic type that is used as a placeholder. After the template portion, the function
swapargs( ) is declared, using X as the data type of the values that will be swapped. In main( ), the
swapargs( ) function is called using three different types of data: ints, floats, and chars. Because
swapargs( ) is a generic function, the compiler automatically creates three versions of swapargs( ): one
that will exchange integer values, one that will exchange floating-point values, and one that will swap
15 C++ A Beginner’s Guide by Herbert Schildt

characters. Thus, the same generic swap( ) function can be used to exchange arguments of any type of
data.
Here are some important terms related to templates. First, a generic function (that is, a function
definition preceded by a template statement) is also called a template function. Both terms are used
interchangeably in this book. When the compiler creates a specific version of this function, it is said to
have created a specialization. This is also called a generated function. The act of generating a function is
referred to as instantiating it. Put differently, a generated function is a specific instance of a template
function.
A Function with Two Generic Types
You can define more than one generic data type in the template statement by using a comma-separated
list. For example, this program creates a template function that has two generic types:
In this example, the placeholder types Type1 and Type2 are replaced by the compiler with the data
types int and char *, and double and long, respectively, when the compiler generates the specific
instances of myfunc( ) within main( ).
Explicitly Overloading a Generic Function
Even though a generic function overloads itself as needed, you can explicitly overload one, too. This is
formally called explicit specialization. If you overload a generic function, then that overloaded function
overrides (or “hides”) the generic function relative to that specific version. For example, consider the
following, revised version of the argument-swapping example shown earlier:
16 C++ A Beginner’s Guide by Herbert Schildt

17 C++ A Beginner’s Guide by Herbert Schildt

18 C++ A Beginner’s Guide by Herbert Schildt

As the comments inside the program indicate, when swapargs(i, j) is called, it invokes the explicitly
overloaded version of swapargs( ) defined in the program. Thus, the compiler does not generate this
version of the generic swapargs( ) function, because the generic function is overridden by the explicit
overloading.
Relatively recently, an alternative syntax was introduced to denote the explicit specialization of a
function. This newer approach uses the template keyword. For example, using the newer specialization
syntax, the overloaded swapargs( ) function from the preceding program looks like this:
As you can see, the new-style syntax uses the template construct to indicate specialization. The type
of data for which the specialization is being created is placed inside the angle brackets following the
function name. This same syntax is used to specialize any type of generic function. While there is no
advantage to using one specialization syntax over the other at this time, the new-style syntax is probably
a better approach for the long term.
Explicit specialization of a template allows you to tailor a version of a generic function to accommodate
a unique situation—perhaps to take advantage of some performance boost that applies to only one type
of data, for example. However, as a general rule, if you need to have different versions of a function for
different data types, you should use overloaded functions rather than templates.
CRITICAL SKILL 12.3: Generic Classes
In addition to using generic functions, you can also define a generic class. When you do this, you create
a class that defines all the algorithms used by that class; however, the actual type of data being
manipulated will be specified as a parameter when objects of that class are created.
Generic classes are useful when a class uses logic that can be generalized. For example, the same
algorithm that maintains a queue of integers will also work for a queue of characters, and the same
mechanism that maintains a linked list of mailing addresses will also maintain a linked list of auto-part
information. When you create a generic class, it can perform the operation you define, such as
maintaining a queue or a linked list, for any type of data. The compiler will automatically generate the
correct type of object, based upon the type you specify when the object is created.
The general form of a generic class declaration is shown here:
19 C++ A Beginner’s Guide by Herbert Schildt

template class class-name {
// body of class }
Here, Ttype is the placeholder type name, which will be specified when a class is instantiated. If
necessary, you can define more than one generic data type using a comma-separated list.
Once you have created a generic class, you create a specific instance of that class using the following
general form:
class-name ob;
Here, type is the type name of the data that the class will be operating upon. Member functions of a
generic class are, themselves, automatically generic. You need not use template to explicitly specify
them as such.
Here is a simple example of a generic class:
The output is shown here:
double division: 3.33333
20 C++ A Beginner’s Guide by Herbert Schildt