This document discusses object oriented programming concepts like exception handling, templates, and template specialization. It provides details on try/catch blocks for exception handling in C++. It also explains that templates allow functions and classes to be defined generically for different data types, and that template specialization allows overriding the default template implementation for a specific type.
3. EXPECTATION HANDLING
• What is expectation?
• Special condition that change normal flow of program execution.
• Provide a way for transferring control.
• C++ support language constructs.
4. EXPECTATION HANDLING
• Built upon three keywords.
• TRY
• CATCH
• Throw
• Catches an exception using a combination of the try and catch keywords.
• try/catch block might generate an expectation.
6. EXPECTATION HANDLING
Writing expectation safe code
• Piece of code is said to be exception-safe, if run-time failures within the code will
not produce ill effects, such as memory leaks.
• Several levels of expectation safety.
• Failure transparency
• Commit or rollback semantics
• Basic exception safety
• Minimal exception safety
• No exception safety
7. WHY EXPECTATION HANDLING?
• Separation of Error Handling code from Normal Code
• Making code maintainable and readable.
• Functions/Methods can handle any exceptions they choose
• In C++, we use built in keyword “throw”.
• Grouping of Error Types
• Create hierarchy of exception objects.
8. TEMPLATE
• What is template?
• Use to define functions as well as classes.
• Use in larger code base for the purpose
of code reusability and flexibility.
• Concept of template used in two different ways
• Function Template
• Class Template
• Member template
9. FUNCTION TEMPLATE
• Function templates are special functions that can operate with generic types.
• Declaration of function template
• Work in a similar to a normal function
10. CLASS TEMPLATE
• Like function template, we can also create class template.
• Sometimes, need a class implementation that is same for all classes,
only the data types used are different.
• Create different class for each data type
OR
Create different variables within a single class.
• Declaration of class template
11. MEMBER TEMPLATE
• Term refer to both member function template and nested class template.
• Member function templates are template functions that are member of a
class.
• Member functions can be function templates in several contexts.
• All functions are generic but not referred to as MFT.
• If these MF take their own template arguments, they are considered to
member function templates.
• MFT of non or template classes are declared as
12. TEMPLATE SPECIALIZATION
• Template is a feature in C++
• Write code once and us it for any data types including user defined.
• Get a special behavior for a particular data type known as
Template Specialization.
13. TEMPLATE SPECIALIZATION
• How does template specialization work?
• Compiler creates a copy of that function/class.
• If specialized version is present, compiler first checks with specialized version
and then main template.
• The idea of template specialization is to override the default template
implementation to handle a particular type in a different way.
1. problem that arises during the execution of a program. such as in coding we add a such a function in which we are dividing numerator by zero.
2. Exception handling is a construct designed to handle the occurrence of exceptions, that is special conditions that changes the normal flow of program execution.
4. one cannot always assume that application/task will run or be completed correctly (exit with the result it was intended to). If this is not the case then ++ supports the use of language constructs to separate error handling and reporting code from ordinary code,
1. C++ exception handling is built upon three keywords: try, catch, and throw.
throw − A program throws an exception when a problem shows up. This is done using a throw keyword.
catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.
try − A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.
2. a method catches an exception using a combination of the try and catch keywords.
3. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch as follows −
also known as the no throw guarantee: Operations are guaranteed to succeed and satisfy all requirements even in presence of exceptional situations.
also known as strong exception safety or no-change guarantee: Operations can fail, but failed operations are guaranteed to have no side effects so all data retain original values.
Partial execution of failed operations can cause side effects.
also known as no-leak guarantee: Partial execution of failed operations may store invalid data but will not cause a crash.
No guarantees are made. (Worst level of exception safety).
In traditional error handling codes, there are always if else conditions to handle errors. This makes the code less readable and maintainable. With try catch blocks, the code for error handling becomes separate from the normal flow.
A function can throw many exceptions, but may choose to handle some of them. The other exceptions which are thrown, but not caught can be handled by caller. If the caller chooses not to catch them, then the exceptions are handled by caller of the caller.
both basic types and objects can be thrown as exception. We can create a hierarchy of exception objects, group exceptions in namespaces or classes, categorize them according to types.
Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.
2. When we write any template based function or class, compiler creates a copy of that function/class whenever compiler sees that being used for a new data type or new set of data types(in case of multiple template arguments).