UNIT - III
C++ PROGRAMMING ADVANCED
FEATURES
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.
• 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.
Syntax for try/catch block:
try
{
// protected code
}
catch( ExceptionName e1 )
{
// catch block
}
catch( ExceptionName e2 )
{
// catch block
}
catch( ExceptionName eN )
{
// catch block
}
Throwing Exceptions
• Exceptions can be thrown anywhere within a
code block using throw statements.
Syntax:
throw(exception);
throw exception;
throw;
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
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)
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;
}
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.
• Object oriented class library:
– It consists of a collection of classes and associated
functions.
– It support common activities, including
• I/O
• Strings
• Numeric processing
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
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
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
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
The standard string template
• <ctype> - functions to determine the type
contained in C data
• <cstring> - character string handling functions
• <string> - handles string class functionalities
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
The standard template library
• Container
– <vector>
– <list>
– <set>
– <multiset>
– <map>
• Iterators :
– <iterator>
Algorithm:
• <algorithm>
The standard I/O libraries
• <iostream>
• <fstream>
• <sstream>
• <iomanip>
• <cstdio>

Unit iii pds

  • 1.
    UNIT - III C++PROGRAMMING ADVANCED FEATURES
  • 2.
    EXCEPTION HANDLING • Anexception 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: Atry 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.
  • 4.
    Syntax for try/catchblock: try { // protected code } catch( ExceptionName e1 ) { // catch block } catch( ExceptionName e2 ) { // catch block } catch( ExceptionName eN ) { // catch block }
  • 5.
    Throwing Exceptions • Exceptionscan be thrown anywhere within a code block using throw statements. Syntax: throw(exception); throw exception; throw;
  • 6.
    Exception handling usingtry, 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() { intx = -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 intmain() { 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 • Standardfunction 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 orientedclass 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 supportlibrary • 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 utilitylibrary • 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 stringtemplate • <ctype> - functions to determine the type contained in C data • <cstring> - character string handling functions • <string> - handles string class functionalities
  • 16.
    Localization classes andtemplate • 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 templatelibrary • Container – <vector> – <list> – <set> – <multiset> – <map>
  • 18.
    • Iterators : –<iterator> Algorithm: • <algorithm>
  • 19.
    The standard I/Olibraries • <iostream> • <fstream> • <sstream> • <iomanip> • <cstdio>