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
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
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
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