SOW_C++_CSO_Chapter_16_9e - Tagged
SOW_C++_CSO_Chapter_16_9e - Tagged
Exceptions and
Templates
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
16.1
Exceptions
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Exceptions
Indicate that something unexpected has
occurred or been detected
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Exceptions - Terminology
Exception: object or value that signals an
error
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Exceptions – Key Words
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Exceptions – Flow of Control
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Exceptions – Example (1)
// function that throws an exception
int totalDays(int days, int weeks)
{
if ((days < 0) || (days > 7))
throw "invalid number of days";
// the argument to throw is the
// character string
else
return (7 * weeks + days);
}
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Exceptions – Example (2)
try // block that calls function
{
totDays = totalDays(days, weeks);
cout << "Total days: " << days;
}
catch (char *msg) // interpret
// exception
{
cout << "Error: " << msg;
}
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Exceptions – What Happens
1) try block is entered. totalDays function is
called
2) If 1st parameter is between 0 and 7, total
number of days is returned and catch block is
skipped over (no exception thrown)
3) If exception is thrown, function and try block
are exited, catch blocks are scanned for 1st
one that matches the data type of the thrown
exception. catch block executes
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
From Program 16-1
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
From Program 16-1
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
What Happens in theTry/Catch
Construct
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
What if no exception is thrown?
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Exceptions - Notes
Predefined functions such as new may
throw exceptions
The value that is thrown does not need to
be used in catch block.
in this case, no name is needed in catch
parameter definition
catch block parameter definition does need
the type of exception being caught
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Exception Not Caught?
An exception will not be caught if
it is thrown from outside of a try block
there is no catch block that matches the data
type of the thrown exception
If an exception is not caught, the program
will terminate
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Exceptions and Objects
An exception class can be defined in a
class and thrown as an exception by a
member function
An exception class may have:
no members: used only to signal an error
members: pass error data to catch block
A class can have more than one exception
class
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Contents of Rectangle.h (Version1) (Continued)
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Program 16-2 (Continued)
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
What Happens After catch
Block?
Once an exception is thrown, the program
cannot return to throw point. The function
executing throw terminates (does not
return), other calling functions in try block
terminate, resulting in unwinding the stack
If objects were created in the try block and
an exception is thrown, they are destroyed.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Nested try Blocks
try/catch blocks can occur within an
enclosing try block
Exceptions caught at an inner level can be
passed up to a catch block at an outer level:
catch ( )
{
...
throw; // pass exception up
} // to next level
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
16.2
Function Templates
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Function Templates
Function template: a pattern for a function
that can work with many data types
When written, parameters are left for the
data types
When called, compiler generates code for
specific data types in function call
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Function Template Example
template
template <class T> prefix
T times10(T num)
generic
{ data type
return 10 * num; type
} parameter
What gets generated when What gets generated when times10 is
times10 is called with an int: called with a double:
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Function Template Example
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Function Template Notes
Can define a template to use multiple data types:
template<class T1, class T2>
Example:
template<class T1, class T2> // T1 and T2 will be
double mpg(T1 miles, T2 gallons) // replaced in the
{ // called function
return miles / gallons // with the data
} // types of the
// arguments
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Function Template Notes
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Function Template Notes
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Function Template Notes
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
16.3
Where to Start When Defining
Templates
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Where to Start
When Defining Templates
Templates are often appropriate for
multiple functions that perform the same
task with different parameter data types
Develop function using usual data types
first, then convert to a template:
add template prefix
convert data type names in the function to a
type parameter (i.e., a T type) in the template
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
16.4
Class Templates
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Class Templates
Classes can also be represented by
templates. When a class object is created,
type information is supplied to define the
type of data members of the class.
Unlike functions, classes are instantiated
by supplying the type name (int, double,
string, etc.) at object definition
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Class Template Example
template <class T>
class grade
{
private:
T score;
public:
grade(T);
void setGrade(T);
T getGrade()
};
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Class Template Example
Pass type information to class template
when defining objects:
grade<int> testList[20];
grade<double> quizList[20];
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Class Templates and
Inheritance
Class templates can inherit from other class templates:
template <class T>
class Rectangle
{ ... };
template <class T>
class Square : public Rectangle<T>
{ ... };
Must use type parameter T everywhere base class
name is used in derived class
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.