0% found this document useful (0 votes)
18 views22 pages

Class Templates and Exceptions

this is the slide for the material sciences for student of masters
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)
18 views22 pages

Class Templates and Exceptions

this is the slide for the material sciences for student of masters
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/ 22

2.

Class templates
and exceptions

1/13
2. Class templates and
exceptions

Class templates

• Provides a specification for generating classes based on parameters.

• 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

What is a template parameter?

• It is a special kind of parameter that allows us to pass a type as argument.

• Instead of passing values to a function, we pass templates, or types, to a function.

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

Class template definition

Class template call

5/13
2. Class templates and
exceptions

Exceptions

 What are exceptions?

Errors in C++ can be categorized in 2 groups:


 Compile Time Errors  Library reference, syntax error or incorrect class
import.
 Run Time Errors  AKA exceptions. They create serious issues.

E.g.: User divides number by zero  An exception will occur  How to deal with it?

6/25
2. Class templates and
exceptions

Exceptions

Why do we need exception handlers?

 It saves time and headaches, as by using an exception handler we can


know what is the problem. Otherwise, we would just know that the
problem exists, but we could not troubleshoot it.

Example of a script that will lead to an error


7/13
2. Class templates and
exceptions

Exceptions – How to deal with them

Types of exception handlers:

We can handle the exception by using the following 3 keywords:

• Try  It detects the error and tries to do something

• Throw  It throws exceptions to an exception handler

• Catch  It catches the exceptions and take appropriate actions

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

We can have only one Try-block for each exception

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

Order of catch statements

• The order of the exceptions is crucial to the execution of the code.

• 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

Exceptions from STL

• The C++ Standard library provides a base class specifically designed to declare objects
to be thrown as exceptions.

• It is called std::exception and is defined in the <exception> header.

• 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

We have placed a handler that catches


exception objects by reference,
therefore this catches also classes
derived from exception, like
our myex object of type myexception.

19/13
2. Class templates and
exceptions

Exceptions from STL

Examples from the STL:

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

You might also like