0% found this document useful (0 votes)
5 views

Templates and Exception Handling

The document discusses the concepts of generic functions and classes in C++, which allow for the creation of algorithms that can operate on various data types. It also covers exception handling, detailing how to manage run-time errors using try, catch, and throw keywords, as well as handling exceptions thrown by memory allocation. Additionally, it includes exercises to implement a templated Stack class, a MathOperation class with error handling, and a custom exception class for negative values.

Uploaded by

Tayef Shahriar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Templates and Exception Handling

The document discusses the concepts of generic functions and classes in C++, which allow for the creation of algorithms that can operate on various data types. It also covers exception handling, detailing how to manage run-time errors using try, catch, and throw keywords, as well as handling exceptions thrown by memory allocation. Additionally, it includes exercises to implement a templated Stack class, a MathOperation class with error handling, and a custom exception class for negative values.

Uploaded by

Tayef Shahriar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Templates and Exception Handling

Contents
• Generic functions

• Generic classes

• Exception handling

• More about exception handling

• Handling exceptions thrown by new

• Exercises
Generic Functions
• Using templates, it is possible to create generic functions and classes.

• A generic function defines a general set of operations that will be applied to various
data types.

• By creating a generic function, you can define, independent of any data, the nature of
the algorithm.

• 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.

• It is used to create template (or framework) that describes what a function will do.
Generic Classes
• In addition to defining generic functions, you can also define generic
classes.

• When you do this, you create a class that defines all algorithms used by that
class, but the actual type of the data being manipulated will be specified as a
parameter when objects of that class are created.

• Generic classes are useful when a class contains generalizable logic.

• Member functions of a generic class are, themselves, automatically generic.


They need not be explicitly specified as such using template.
Exception Handling
• C++ provides a built-in error handling mechanism that is called exception handling.

• Using exception handling, you can more easily manage and respond to run-time
errors.

• C++ exception handling is built upon three keywords: try, catch, and throw.

• If an exception (i.e., an error) occurs within the try block, it is thrown (using
throw). The exception is caught, using catch, and processed.

• The try block must contain the portion of your program that you want to monitor
for errors.
More About Exception Handling
• In some circumstances you will want an exception handler to catch all
exceptions instead of just a certain type. This is easy to accomplish. Simply
use this form of catch:
catch(…){
//process all exceptions

• You can control what type of exceptions a function can throw outside of itself.

• To apply these restrictions, you must add a throw clause to the function
definition.
Handling Exceptions Thrown By new
• In C++, when an allocation request cannot be honored, new throws a
bad_alloc exception.

• If you don’t catch this exception, your program will be terminated.

• To have access to this exception, you must include header <new> in your
program.
Exercises
• Create a templated Stack class that can store elements of any data type (e.g.,
int, double, string). Implement push, pop, and top functions. Test it with
various data types.

• Create a class MathOperation that has a method for dividing two numbers.
Use exception handling to catch and handle the division by zero error.

• Design a custom exception class called NegativeValueException. Use it to


handle situations where a negative value is not allowed, like in a class that
represents a bank account balance.

You might also like