SlideShare a Scribd company logo
Unit v
Exception handling
 Exception refers to unexpected condition in the
program.
 The unusual condition could be faults which may
cause the failure of a system.
 The error handling mechanism is called as exception
handling.
 Exceptions are classified into synchronous and asynchronous
exceptions.
 The exceptions which occur during the program execution, due to
some fault in the input-data or technique that is not suitable to
handle the current class of data, within the program, arc known as
synchronous exceptions.
 For instance, errors such as out-of-range, overflow, underflow, and
so on belong to the class of synchronous exceptions.
 The exceptions caused by events or faults unrelated (external) to
the program and beyond the control of program are called
asynchronous exceptions. For instance, errors such as keyboard
interrupts, hardware malfunctions, disk failure, and so on belong to
the class of asynchronous exceptions.
 The proposed exception handling mechanism in C++ is designed to
handle only synchronous exceptions caused within a program.
Exception Handling Model
 When a program encounters an abnormal situation for which it is
not designed, the user may transfer control to some other part of
the program that is designed to deal with the problem. This is
done by throwing an exception.
 The exception-handling mechanism uses three blocks: try, throw,
and catch.
Unit v
throw Construct
catch Construct
try Construct
 The mechanism suggests that error handling code must perform
the following tasks.
 Detect the problem causing exception (Hit the exception)
 Inform that an error has occurred (Throw the exception)
 Receive the error information (Catch the exception)
 Take corrective actions (Handle the exceptions)
Handler Throwing the Same Exception Again
 List of Exceptions
Exceptions in Constructors and Destructors
 Example
Handling Uncaught Exceptions
 The uncaught exception handling mechanism relies on two library
functions, terminate() and unexpected ( ) ,for coping with
exceptions unhandled explicitly. C++ supports the following
special functions to handle uncaught exceptions in a systematic
manner:
 terminate()
 set_terminate ( )
 unexpected()
 set_unexpected()
terminate()
The function terminate () is invoked when an exception is
raised and the handler is not found. The default action for
terminate is to invoke abort (). Such a default action causes
immediate termination of the program execution.
 Example
set_terminate()
The set_terminate function allows the user to install a
function that defines the program’s actions to be taken to
terminate the program when a handler for the exception
cannot be found. The actions are defined in t_func, which
is declared to be a function of type terminate_function. A
terminate_function type defined in except .h/exception, is
a function that takes no arguments, and returns nothing.
Example
unexpected()
 The unexpected function is called when a function throws an
exception not listed in its exception specification. The program
calls unexpected () which calls any user-defined function
registered by set_unexpected. If no function is registered with
set_unexpected, the unexpected() function then invokes the
terminate () function.
 Example
set_unexpected()
The function set_unexpected() lets the user to install a
function that defines the program’s actions to be taken
when a function throws an exception not listed in its
exception specification. The actions are defined in
unexpected_func() library function. By default, an
unexpected exception causes unexpected() to be called,
which in turn calls unexpected_func.
 Example
Namespace
 Example
A namespace (sometimes also called a name scope) is an abstract container or
environment created to hold a logical grouping of unique identifiers or symbols
(i.e., names). An identifier defined in a namespace is associated only with that
namespace. The same identifier can be independently defined in multiple
namespaces.
Introduction to the STL
 The STL contains several kinds of entities. The three most
important are
 containers,
algorithms, and
iterators.
Container
 A container is a way that stored data is organized in memory.
 Arrays, Stacks , Queue etc.
 The STL containers are implemented by template classes, so they
can be easily customized to hold different kinds of data.
Algorithms
Algorithms in the STL are procedures that are applied to
containers to process their data in various ways. For
example, there are algorithms to sort. copy, search, and
merge data.
Algorithms are represented by template functions. These
functions are not member functions of the container
classes. Rather they are standalone functions.
Iterators
Iterators are a generalization of the concept of pointers:
they point to elements in a container.
You can increment an iterator, as you can a pointer, so it
points in turn to each element in a container.
Iterators are a key part of the STL because they connect
algorithms with containers.
Unit v
Containers…………
Containers in the STL fall into two main categories:
sequence and associative.
The sequence containers are vector. list, and deque.
The associative containers are set. multiset, map. and
multimap.
In addition, several specialized containers are derived from
the sequence containers.
Sequence Container
A sequence container stores a set of elements in what you
can visualize as a line
Each element is related to the other elements by its
position along the line.
Each element (except at the ends) is preceded by one
specific element and followed by another.
An ordinary C++ array is an example of a sequence
container.
Unit v
Unit v
Associative Containers
An associative container is not sequential: instead it uses
keys to access data.
The keys, typically numbers or stings, are used
automatically by the container to arrange the stored
elements in a specific order.
It’s like an ordinary English dictionary, in which you access
data by looking up words arranged in alphabetical order.
and the container converts this key to the element’s
location in memory. If you know the key. you can access the
associated value swiftly.
There are two kinds of associative containers in the STL:
sets and maps.
These both store data in a structure called a tree, which
offers fast searching, insertion, and deletion. Sets and maps
are thus very versatile general data structures suitable for a
wide variety of applications.
However, it is inefficient to son them and perform other
operations that require random access.
Unit v
Unit v
Member functions
Container Adapters
 Vector Example
 Deque Example
 Set Example
Algorithms
Algorithm
 find()
 Count()
 Sort()
 Search()
 Merge()
Function objects
Some algorithms can take something called a function
object as an argument. A function object looks, to the user,
much like a template function. However, it's actually an
object of a template class that has a single member
function: the overloaded () operator.
Example
User-written Functions in Place of Function Objects
 Example
Member Functions swap(), emptv(), back() and pop back()
 Example
Iterators
 Iterators can be used as smart pointers and
connection between algorithm and containers.
 Smart pointers:
 float* ptr = start_address
for(int j=0; j<SIZE; j++)
cout <<*ptr++;
 Example
Unit v
Reverse Iterators
Stream Iterators
 Stream iterators allow you to treat files and I/O devices (such as
cm and cout) as if they were iterators. This makes it easy to use
files and I/O devices as arguments to algorithms.
 The major purpose of the input and output iterator categories is to
support these stream iterator classes.
 Stream iterators are actually objects of classes that are templetized
for different types of input or output. There are two stream
iterators: ostream_iterator and istream_iterator.
 Example
Storing Using defined objects
 Example

More Related Content

PPT
Generics Collections
PDF
Using Static Analysis in Program Development
PPT
Java Annotation
PDF
Beginning linq
PDF
Java collections-interview-questions
PDF
Java Collections
PPT
Ch14
Generics Collections
Using Static Analysis in Program Development
Java Annotation
Beginning linq
Java collections-interview-questions
Java Collections
Ch14

What's hot (20)

PDF
Arrays Fundamentals Unit II
PPTX
Exploiting JXL using Selenium
PPTX
Java.util
PPT
statement interface
ODP
BIS06 Physical Database Models
PPTX
java programming basics - part ii
PPT
8 if
PPTX
Oracle: Cursors
PDF
Server-Solvers-Interacter-Interfacer-Modeler-Presolver Libraries and Executab...
PPSX
Net framework session01
PPTX
Sql server ___________session_18(stored procedures)
PDF
Java Collections Tutorials
PPTX
WEKA: Data Mining Input Concepts Instances And Attributes
PPTX
Java Tutorial Lab 2
DOC
PPTX
List interface in collections framework
PPT
java collections
PDF
IO Streams, Serialization, de-serialization, autoboxing
PPTX
Generic Programming &amp; Collection
Arrays Fundamentals Unit II
Exploiting JXL using Selenium
Java.util
statement interface
BIS06 Physical Database Models
java programming basics - part ii
8 if
Oracle: Cursors
Server-Solvers-Interacter-Interfacer-Modeler-Presolver Libraries and Executab...
Net framework session01
Sql server ___________session_18(stored procedures)
Java Collections Tutorials
WEKA: Data Mining Input Concepts Instances And Attributes
Java Tutorial Lab 2
List interface in collections framework
java collections
IO Streams, Serialization, de-serialization, autoboxing
Generic Programming &amp; Collection
Ad

Viewers also liked (6)

PPTX
App Promotion Summit: leverage reviews for ASO
PDF
ASOBarcamp 2 - Ratings and Reviews - ASO Impact and Leverage
PPTX
Reviews & Ratings - Thomasbcn 2016 @ Applause.io
PDF
TargetSummit Berlin Meetup | 8Fit, Thomas Petit
PPTX
ASO: A/B Testing your store listing
PPTX
Apple Search Ads - App Promotion Summit Berlin 2016
App Promotion Summit: leverage reviews for ASO
ASOBarcamp 2 - Ratings and Reviews - ASO Impact and Leverage
Reviews & Ratings - Thomasbcn 2016 @ Applause.io
TargetSummit Berlin Meetup | 8Fit, Thomas Petit
ASO: A/B Testing your store listing
Apple Search Ads - App Promotion Summit Berlin 2016
Ad

Similar to Unit v (20)

PDF
C++ Advanced Features
PDF
C++ Advanced Features
PPT
UNIT III.ppt
PPT
UNIT III (2).ppt
DOCX
unit 5.docx...............................
PPT
Savitch Ch 18
PPTX
Lecture 1 Try Throw Catch.pptx
PPTX
CMSC 202 - Lec20 - Containers and Iterators(2).pptx
PDF
PDF
PPT
Unit iii
PPTX
How to Adopt Modern C++17 into Your C++ Code
PPTX
How to Adopt Modern C++17 into Your C++ Code
PPTX
Object Oriented Design and Programming Unit-04
PPT
Savitch ch 18
PDF
STL in C++
PPTX
Standard Template Library
PDF
C++ How to Program 10th Edition Deitel Solutions Manual
C++ Advanced Features
C++ Advanced Features
UNIT III.ppt
UNIT III (2).ppt
unit 5.docx...............................
Savitch Ch 18
Lecture 1 Try Throw Catch.pptx
CMSC 202 - Lec20 - Containers and Iterators(2).pptx
Unit iii
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ Code
Object Oriented Design and Programming Unit-04
Savitch ch 18
STL in C++
Standard Template Library
C++ How to Program 10th Edition Deitel Solutions Manual

More from donny101 (9)

PPTX
Unit iv
PPTX
Unit iii
PPTX
Unit ii
PPTX
Unit 1
PDF
Unit vos - File systems
PDF
Unit ivos - file systems
PDF
Unit iios process scheduling and synchronization
PDF
Unit iiios Storage Management
PDF
Unit 1os processes and threads
Unit iv
Unit iii
Unit ii
Unit 1
Unit vos - File systems
Unit ivos - file systems
Unit iios process scheduling and synchronization
Unit iiios Storage Management
Unit 1os processes and threads

Recently uploaded (20)

PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Geodesy 1.pptx...............................................
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Welding lecture in detail for understanding
PPTX
Construction Project Organization Group 2.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPT
Project quality management in manufacturing
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
PPT on Performance Review to get promotions
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPT
Mechanical Engineering MATERIALS Selection
DOCX
573137875-Attendance-Management-System-original
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Geodesy 1.pptx...............................................
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Lecture Notes Electrical Wiring System Components
Welding lecture in detail for understanding
Construction Project Organization Group 2.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Project quality management in manufacturing
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
CYBER-CRIMES AND SECURITY A guide to understanding
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT on Performance Review to get promotions
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Mechanical Engineering MATERIALS Selection
573137875-Attendance-Management-System-original

Unit v

  • 2. Exception handling  Exception refers to unexpected condition in the program.  The unusual condition could be faults which may cause the failure of a system.  The error handling mechanism is called as exception handling.
  • 3.  Exceptions are classified into synchronous and asynchronous exceptions.  The exceptions which occur during the program execution, due to some fault in the input-data or technique that is not suitable to handle the current class of data, within the program, arc known as synchronous exceptions.  For instance, errors such as out-of-range, overflow, underflow, and so on belong to the class of synchronous exceptions.  The exceptions caused by events or faults unrelated (external) to the program and beyond the control of program are called asynchronous exceptions. For instance, errors such as keyboard interrupts, hardware malfunctions, disk failure, and so on belong to the class of asynchronous exceptions.  The proposed exception handling mechanism in C++ is designed to handle only synchronous exceptions caused within a program.
  • 4. Exception Handling Model  When a program encounters an abnormal situation for which it is not designed, the user may transfer control to some other part of the program that is designed to deal with the problem. This is done by throwing an exception.  The exception-handling mechanism uses three blocks: try, throw, and catch.
  • 9.  The mechanism suggests that error handling code must perform the following tasks.  Detect the problem causing exception (Hit the exception)  Inform that an error has occurred (Throw the exception)  Receive the error information (Catch the exception)  Take corrective actions (Handle the exceptions)
  • 10. Handler Throwing the Same Exception Again  List of Exceptions
  • 11. Exceptions in Constructors and Destructors  Example
  • 12. Handling Uncaught Exceptions  The uncaught exception handling mechanism relies on two library functions, terminate() and unexpected ( ) ,for coping with exceptions unhandled explicitly. C++ supports the following special functions to handle uncaught exceptions in a systematic manner:  terminate()  set_terminate ( )  unexpected()  set_unexpected()
  • 13. terminate() The function terminate () is invoked when an exception is raised and the handler is not found. The default action for terminate is to invoke abort (). Such a default action causes immediate termination of the program execution.  Example
  • 14. set_terminate() The set_terminate function allows the user to install a function that defines the program’s actions to be taken to terminate the program when a handler for the exception cannot be found. The actions are defined in t_func, which is declared to be a function of type terminate_function. A terminate_function type defined in except .h/exception, is a function that takes no arguments, and returns nothing. Example
  • 15. unexpected()  The unexpected function is called when a function throws an exception not listed in its exception specification. The program calls unexpected () which calls any user-defined function registered by set_unexpected. If no function is registered with set_unexpected, the unexpected() function then invokes the terminate () function.  Example
  • 16. set_unexpected() The function set_unexpected() lets the user to install a function that defines the program’s actions to be taken when a function throws an exception not listed in its exception specification. The actions are defined in unexpected_func() library function. By default, an unexpected exception causes unexpected() to be called, which in turn calls unexpected_func.  Example
  • 17. Namespace  Example A namespace (sometimes also called a name scope) is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols (i.e., names). An identifier defined in a namespace is associated only with that namespace. The same identifier can be independently defined in multiple namespaces.
  • 18. Introduction to the STL  The STL contains several kinds of entities. The three most important are  containers, algorithms, and iterators.
  • 19. Container  A container is a way that stored data is organized in memory.  Arrays, Stacks , Queue etc.  The STL containers are implemented by template classes, so they can be easily customized to hold different kinds of data.
  • 20. Algorithms Algorithms in the STL are procedures that are applied to containers to process their data in various ways. For example, there are algorithms to sort. copy, search, and merge data. Algorithms are represented by template functions. These functions are not member functions of the container classes. Rather they are standalone functions.
  • 21. Iterators Iterators are a generalization of the concept of pointers: they point to elements in a container. You can increment an iterator, as you can a pointer, so it points in turn to each element in a container. Iterators are a key part of the STL because they connect algorithms with containers.
  • 23. Containers………… Containers in the STL fall into two main categories: sequence and associative. The sequence containers are vector. list, and deque. The associative containers are set. multiset, map. and multimap. In addition, several specialized containers are derived from the sequence containers.
  • 24. Sequence Container A sequence container stores a set of elements in what you can visualize as a line Each element is related to the other elements by its position along the line. Each element (except at the ends) is preceded by one specific element and followed by another. An ordinary C++ array is an example of a sequence container.
  • 27. Associative Containers An associative container is not sequential: instead it uses keys to access data. The keys, typically numbers or stings, are used automatically by the container to arrange the stored elements in a specific order. It’s like an ordinary English dictionary, in which you access data by looking up words arranged in alphabetical order. and the container converts this key to the element’s location in memory. If you know the key. you can access the associated value swiftly.
  • 28. There are two kinds of associative containers in the STL: sets and maps. These both store data in a structure called a tree, which offers fast searching, insertion, and deletion. Sets and maps are thus very versatile general data structures suitable for a wide variety of applications. However, it is inefficient to son them and perform other operations that require random access.
  • 33.  Vector Example  Deque Example  Set Example
  • 35. Algorithm  find()  Count()  Sort()  Search()  Merge()
  • 36. Function objects Some algorithms can take something called a function object as an argument. A function object looks, to the user, much like a template function. However, it's actually an object of a template class that has a single member function: the overloaded () operator. Example
  • 37. User-written Functions in Place of Function Objects  Example Member Functions swap(), emptv(), back() and pop back()  Example
  • 38. Iterators  Iterators can be used as smart pointers and connection between algorithm and containers.  Smart pointers:  float* ptr = start_address for(int j=0; j<SIZE; j++) cout <<*ptr++;  Example
  • 41. Stream Iterators  Stream iterators allow you to treat files and I/O devices (such as cm and cout) as if they were iterators. This makes it easy to use files and I/O devices as arguments to algorithms.  The major purpose of the input and output iterator categories is to support these stream iterator classes.  Stream iterators are actually objects of classes that are templetized for different types of input or output. There are two stream iterators: ostream_iterator and istream_iterator.  Example
  • 42. Storing Using defined objects  Example