SlideShare a Scribd company logo
1 of 32
Object Oriented Programming using C++
Module No – 3
Exception Handling and File and IO
Streams
Subject :Object Oriented Programming using C++
Contents
◻ Exception Handling: Introduction, Exception Handling Mechanism -
try, catch and throw, Multiple Exceptions.
◻ File and IO Streams: Stream and Files, Stream Classes, File
Pointers,
◻ File I/O with Member Functions. Case study/examples in C++.
3
◻ Introduction
◻ First of all, we will understand the meaning of the exception. See there are 3 types of errors that
are occurred in programming, they are as follows:
◻ 1. Syntax Error:
◻ While typing the program, if the programmer mistypes something or did not write something
properly then there is an error known as Syntax Error
◻ 2. Logical Error:
◻ Suppose you wanted to do something and so wrote the procedure or function or some code but
when you run the program the results are different i.e. not as expected.
◻ 3. Runtime Error:
◻ at runtime, you are facing errors.
• What are Exceptions?
• An exception is nothing but a situation in which we get the runtime error.
4
try, catch, throw
How to Implement Exception Handling in C++?
1. try: It represents a block of code that can throw an exception.
2. catch: It represents a block of code that is executed when a particular exception
is thrown.
3. throw: It is used to throw an exception inside a try block.
Multiple catch blocks
● The following example shows the use of multiple catch blocks to
handle different types of exceptions.
● The following example code is self-explanatory.
● If the value of an integer in the array arr is less than 0, we are
throwing a numeric value as an exception and if the value is
greater than 0, then we are throwing a character value as an
exception.
● And we have two different catch blocks to catch those exceptions.
What is a file?
• A computer file is a computer resource for recording data discretely
in a computer storage device.
• Many real-life problems handle large volumes of data.
• The data is stored in the devices using the concept of files.
• A file is a collection of related data stored in a particular area on the
disk.
• Programs are designed to perform read and write operations on
these files.
Stream
• A stream is a flow of data or a flow of characters.
• Streams are used for accessing the data from outside the program that
is from external sources or destinations.
• So, data can be transferred from an external source to a program or
from a program to a destination that is an external device.
• From accessing the data from outside the program we use streams
Classes for File Stream Operations
Classes for File Stream Operations
File opening modes
• In C++, for every file operation, exists a specific file mode.
• These file modes allow us to create, read, write, append or modify
a file.
• The file modes are defined in the class ios.
• A File stream act as an interface between the program and the
files.
Default opening modes of a file
• For ifstream ----- ios::in
• For ofstream ------ ios::out
• For fstream --------- ios::in | ios::out
Opening a File
Opening a File Program
Closing a File
Closing a File Program
Writing into a File
• While programming in C++, the information to be written into a
file uses the stream insertion operator (<<) operator to output
information to the screen.
• The only difference is that you use an ofstream or fstream
object instead of the cout object.
Reading from a File
• The information can be read from a file into another program
using the stream extraction operator (>>)
• The only difference is that you use an ifstream or fstream
object instead of the cin object
null.pptx
null.pptx
null.pptx
null.pptx

More Related Content

Similar to null.pptx

Similar to null.pptx (20)

Filehadnling
FilehadnlingFilehadnling
Filehadnling
 
31cs
31cs31cs
31cs
 
32sql server
32sql server32sql server
32sql server
 
Linux System Programming - File I/O
Linux System Programming - File I/O Linux System Programming - File I/O
Linux System Programming - File I/O
 
CPP17 - File IO
CPP17 - File IOCPP17 - File IO
CPP17 - File IO
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
 
File management in C++
File management in C++File management in C++
File management in C++
 
Advanced programming ch2
Advanced programming ch2Advanced programming ch2
Advanced programming ch2
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
Intro To C++ - Class #21: Files
Intro To C++ - Class #21: FilesIntro To C++ - Class #21: Files
Intro To C++ - Class #21: Files
 
Streams and Files
Streams and FilesStreams and Files
Streams and Files
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
Data file handling
Data file handlingData file handling
Data file handling
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O
 
INTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c languageINTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c language
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
 

Recently uploaded

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
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
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
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
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
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 

Recently uploaded (20)

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
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
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
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
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
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
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...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 

null.pptx

  • 2. Module No – 3 Exception Handling and File and IO Streams Subject :Object Oriented Programming using C++
  • 3. Contents ◻ Exception Handling: Introduction, Exception Handling Mechanism - try, catch and throw, Multiple Exceptions. ◻ File and IO Streams: Stream and Files, Stream Classes, File Pointers, ◻ File I/O with Member Functions. Case study/examples in C++. 3
  • 4. ◻ Introduction ◻ First of all, we will understand the meaning of the exception. See there are 3 types of errors that are occurred in programming, they are as follows: ◻ 1. Syntax Error: ◻ While typing the program, if the programmer mistypes something or did not write something properly then there is an error known as Syntax Error ◻ 2. Logical Error: ◻ Suppose you wanted to do something and so wrote the procedure or function or some code but when you run the program the results are different i.e. not as expected. ◻ 3. Runtime Error: ◻ at runtime, you are facing errors. • What are Exceptions? • An exception is nothing but a situation in which we get the runtime error. 4
  • 5.
  • 6. try, catch, throw How to Implement Exception Handling in C++?
  • 7. 1. try: It represents a block of code that can throw an exception. 2. catch: It represents a block of code that is executed when a particular exception is thrown. 3. throw: It is used to throw an exception inside a try block.
  • 8.
  • 9.
  • 10. Multiple catch blocks ● The following example shows the use of multiple catch blocks to handle different types of exceptions. ● The following example code is self-explanatory. ● If the value of an integer in the array arr is less than 0, we are throwing a numeric value as an exception and if the value is greater than 0, then we are throwing a character value as an exception. ● And we have two different catch blocks to catch those exceptions.
  • 11.
  • 12. What is a file? • A computer file is a computer resource for recording data discretely in a computer storage device. • Many real-life problems handle large volumes of data. • The data is stored in the devices using the concept of files. • A file is a collection of related data stored in a particular area on the disk. • Programs are designed to perform read and write operations on these files.
  • 13. Stream • A stream is a flow of data or a flow of characters. • Streams are used for accessing the data from outside the program that is from external sources or destinations. • So, data can be transferred from an external source to a program or from a program to a destination that is an external device. • From accessing the data from outside the program we use streams
  • 14.
  • 15. Classes for File Stream Operations
  • 16. Classes for File Stream Operations
  • 17.
  • 18.
  • 19.
  • 20. File opening modes • In C++, for every file operation, exists a specific file mode. • These file modes allow us to create, read, write, append or modify a file. • The file modes are defined in the class ios. • A File stream act as an interface between the program and the files.
  • 21. Default opening modes of a file • For ifstream ----- ios::in • For ofstream ------ ios::out • For fstream --------- ios::in | ios::out
  • 23. Opening a File Program
  • 25. Closing a File Program
  • 26. Writing into a File • While programming in C++, the information to be written into a file uses the stream insertion operator (<<) operator to output information to the screen. • The only difference is that you use an ofstream or fstream object instead of the cout object.
  • 27.
  • 28. Reading from a File • The information can be read from a file into another program using the stream extraction operator (>>) • The only difference is that you use an ifstream or fstream object instead of the cin object