Class Templates and Exceptions
Class Templates and Exceptions
Class templates
and exceptions
1/13
2. Class templates and
exceptions
Class templates
• The main idea is to pass data type as a parameter so that we don’t need to write the same
code for different data types
2/13
2. Class templates and
exceptions
Class templates
3/13
2. Class templates and
exceptions
Class templates
4/13
2. Class templates and
exceptions
Class templates
Instantiation of templates
We instantiate class templates to create objects that have some parameters that we want
5/13
2. Class templates and
exceptions
Exceptions
E.g.: User divides number by zero An exception will occur How to deal with it?
6/25
2. Class templates and
exceptions
Exceptions
8/13
2. Class templates and
exceptions
Exceptions - Types
Try-block
The code that can throw any exception is kept inside a try block.
The main function of Try-block is to find the error (hit the exception) and then throw it
to the catch-block
9/13
2. Class templates and
exceptions
Exceptions - Types
Throw exceptions
It is a part of Try-block
Its main function is to inform about the occurence of the exception to
the exception handler (Catch-block)
Basically, it throws the error notice to the Catch-block.
10/13
2. Class templates and
exceptions
Exceptions - Types
Catch block
Catch block is intended to catch the error and handle the exception
condition.
11/13
2. Class templates and
exceptions
Example
Following is a simple example to show exception handling in C++. The output of program explains flow of execution of try/catch blocks.
12/13
2. Class templates and
exceptions
There is a special catch block called ‘catch all’ catch(…) that can be used to catch all types of exceptions. For example, in the following program,
an int is thrown as an exception, but there is no catch block for int, so catch(…) block will be executed.
13/13
2. Class templates and
exceptions
If an exception is thrown and not caught anywhere, the program terminates abnormally. For example, in the following
program, a char is thrown, but there is no catch block to catch a char.
14/13
2. Class templates and
exceptions
In C++, try-catch blocks can be nested. Also, an exception can be re-thrown using “throw; ”
15/13
2. Class templates and
exceptions
• When the compiler encounters an exception in a Try-block, it will try each handler in order of
appearance.
• A catch block of the form catch(...) must be the last catch block following a try block or an error
occurs. This placement ensures that the catch(...) block does not prevent more specific catch
blocks from catching exceptions intended for them.
• If the run time cannot find a matching handler in the current scope, the run time will continue to
find a matching handler in a dynamically surrounding try block.
16/13
2. Class templates and
exceptions
17/13
2. Class templates and
exceptions
• The C++ Standard library provides a base class specifically designed to declare objects
to be thrown as exceptions.
• This class has a virtual member function called what that returns a null-terminated
character sequence (of type char *) and that can be overwritten in derived classes to
contain some sort of description of the exception.
18/13
2. Class templates and
exceptions
19/13
2. Class templates and
exceptions
exception description
bad_alloc Below are some examples of exception classes
thrownfrom the standard
by new library:
on allocation failure
thrown by dynamic_cast when it fails in a dynamic
bad_cast
std::bad_exception <exception> cast an incorrect exception was
signifies
bad_exceptionthrown thrown by certain dynamic exception specifiers
bad_typeid std::bad_alloc <new> failure to allocate storage
thrown by typeid
bad_function_call
std::bad_array_new_length thrown
<new> invalid bylength
array empty function objects
bad_weak_ptrstd::overflow_error thrownoverflow
<stdexcept> arithmetic by shared_ptr
errorwhen passed a bad weak_ptr
20/13
2. Class templates and
exceptions
Example of bad_alloc
21/13
2. Class templates and
exceptions
Thank you!
22/13