2. EXCEPTION HANDLING
• An exception is a problem that arises during the
execution of a program. A C++ exception is a
response to an exceptional circumstance that arises
while a program is running, such as an attempt to
divide by zero.
• Exceptions provide a way to transfer control from
one part of a program to another.
• C++ exception handling is built upon three
keywords: try, catch and throw.
3. • 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.
• 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.
• throw: A program throws an exception when a
problem shows up. This is done using
a throw keyword.
5. Throwing Exceptions
• Exceptions can be thrown anywhere within a
code block using throw statements.
Syntax:
throw(exception);
throw exception;
throw;
6. Exception handling using try, throw
and catch expression
Try block
Involves a function which
contains an exception
Catch block
Catches and handles the
exception
Throw block
Function that causes
an exceprion
7. Example Program:
int main()
{
int x = -1;
cout << "Before try n";
try {
cout << "Inside try n";
if (x < 0)
{
throw x;
cout << "After throw (Never executed) n";
}
}
catch (int x ) {
cout << "Exception Caught n";
}
cout << "After catch (Will be executed) n";
return 0;
}
Output:
Before try
Inside try
Exception Caught
After catch (Will be executed)
8. Divide by Zero
int main()
{
int dividend, divisor, quotient;
try
{
cout<<“enter the dividend:”;
cin>>dividend;
cout<<“enter the divisor:”;
cin>>divisor;
if(divisor==0)
throws divisor;
quotient=dividend/divisor;
Cout<<“Quotient=“<<quotent;
}
catch(int)
{
cout<<“Division by Zero”;
}
return 0;
}
9. STANDARD LIBRARIES
• Standard function library:
– It consists of general-purpose and stand alone
functions that are not part of any class.
– These functions perform essential services such as
• Input
• Output
• Implementation of other operations
It is inherited from C.
10. • Object oriented class library:
– It consists of a collection of classes and associated
functions.
– It support common activities, including
• I/O
• Strings
• Numeric processing
11. The standard C++ library includes
• The language support library
• The diagnostics library
• The general utility library
• The standard string template
• Localization classes and template
• The standard template library
• The standard numerics library
• The standard I/O library
• C++ header for the standard C library
12. The language support library
• It defines types and functions that will be used
implicitly by c++ programs.
• <new> - low level memory management utilities
• <memory> - higher level memory management
utilities
• <delete> - to deallocate the memory
• <exception> - exception handling utilities
• <typeinfo> - runtime information utilities
13. The diagnostics library
• Used to detect and report error conditions
• <stdexcept> - standard exception objects
• <system_error> - a platform_dependent error
code
• <cerrno> - macro containing the last error
number
14. The general utility library
• Especially by the STL which includes
Containers, Iterators and Algorithms libraries.
• <utility> - including utility components
• <cstdlib> - general purpose utilities:random
numbers, program control, sorting
and searching
• <ctime> - C style time/date utilities
15. The standard string template
• <ctype> - functions to determine the type
contained in C data
• <cstring> - character string handling functions
• <string> - handles string class functionalities
16. Localization classes and template
• Localization library permits a C++ program to
address the cultural differences of its various
users.
• <locale> - Localization utilities
• <clocale> - C Localization utilities
• <codecvt> - unicode convertion facilities
17. The standard template library
• Container
– <vector>
– <list>
– <set>
– <multiset>
– <map>