SlideShare a Scribd company logo
1 of 19
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>

More Related Content

What's hot

0.my book draft chap 1
0.my book draft chap 10.my book draft chap 1
0.my book draft chap 1manhduc1811
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingPython Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingP3 InfoTech Solutions Pvt. Ltd.
 
[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3Seok-joon Yun
 
O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...
O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...
O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...Rodolfo Carvalho
 
2.Format Strings
2.Format Strings2.Format Strings
2.Format Stringsphanleson
 

What's hot (9)

0.my book draft chap 1
0.my book draft chap 10.my book draft chap 1
0.my book draft chap 1
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingPython Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception Handling
 
[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3
 
O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...
O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...
O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...
 
2.Format Strings
2.Format Strings2.Format Strings
2.Format Strings
 
Hacking 101
Hacking 101Hacking 101
Hacking 101
 
Tu1
Tu1Tu1
Tu1
 
Simware framework hello world: A webinar
Simware framework hello world: A webinarSimware framework hello world: A webinar
Simware framework hello world: A webinar
 
Lab 3
Lab 3Lab 3
Lab 3
 

Similar to Unit iii pds (20)

Unit iii
Unit iiiUnit iii
Unit iii
 
UNIT III.ppt
UNIT III.pptUNIT III.ppt
UNIT III.ppt
 
UNIT III (2).ppt
UNIT III (2).pptUNIT III (2).ppt
UNIT III (2).ppt
 
Exceptionn
ExceptionnExceptionn
Exceptionn
 
Exception
ExceptionException
Exception
 
Exception
ExceptionException
Exception
 
Exception
ExceptionException
Exception
 
Exception
ExceptionException
Exception
 
Exception
ExceptionException
Exception
 
Exception
ExceptionException
Exception
 
Exception
ExceptionException
Exception
 
Exception
ExceptionException
Exception
 
Exception
ExceptionException
Exception
 
Exception handling
Exception handlingException handling
Exception handling
 
43c
43c43c
43c
 
Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptx
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Handling
HandlingHandling
Handling
 
6-Exception Handling and Templates.pptx
6-Exception Handling and Templates.pptx6-Exception Handling and Templates.pptx
6-Exception Handling and Templates.pptx
 
Java Day-5
Java Day-5Java Day-5
Java Day-5
 

Recently uploaded

(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 

Recently uploaded (20)

(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 

Unit iii pds

  • 1. UNIT - III C++ PROGRAMMING ADVANCED FEATURES
  • 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.
  • 4. Syntax for try/catch block: try { // protected code } catch( ExceptionName e1 ) { // catch block } catch( ExceptionName e2 ) { // catch block } catch( ExceptionName eN ) { // catch block }
  • 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>
  • 18. • Iterators : – <iterator> Algorithm: • <algorithm>
  • 19. The standard I/O libraries • <iostream> • <fstream> • <sstream> • <iomanip> • <cstdio>