0% found this document useful (0 votes)
7 views16 pages

Exceptions and file handling

The document covers advanced concepts in Object-Oriented Programming (OOP) focusing on exceptions and file handling. It explains the syntax and types of exceptions, their handling using try, catch, and throw keywords, as well as the various classes and modes associated with file handling in C++. Key points include the importance of proper exception handling and the different file modes for reading and writing operations.

Uploaded by

harshvardhanagam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

Exceptions and file handling

The document covers advanced concepts in Object-Oriented Programming (OOP) focusing on exceptions and file handling. It explains the syntax and types of exceptions, their handling using try, catch, and throw keywords, as well as the various classes and modes associated with file handling in C++. Key points include the importance of proper exception handling and the different file modes for reading and writing operations.

Uploaded by

harshvardhanagam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

FY- OOP- Programming for Problem Solving

Prof. Sarang Joshi


OOP Advanced Concepts: Exceptions and File
Handling (F015)
Module IV
Exceptions
⚫ Keyword: try, throw, catch
⚫ Exceptions are run time abnormalities or anomalies or test patches which needs
to be detected and resolved.
⚫ A try provides opportunity to write a code to test it or a debug or resolve a
problem or exceptional condition.
⚫ A catch, catches exception generated in a code of “try” by throw and provides a
code that resolves the exception. There can be one or more catch blocks in a
try. For catch all syntax is catch(…) or it is also called as default catch. Multiple
catch are also possible with multiple catch statements with different arguments
or passing parameters.
⚫ A throw sends a signal or detects anomalies when an exceptional condition
occurs in code under test in try block, immediately terminates current execution
of try code and selects appropriate catch block to process or resolve the
exception.

Exception Syntax
⚫ try{

− // A code to be tested// suspected anomaly

− throw ExceptionType_Number(“Error Messages”);

⚫ }
⚫ Catch (Exception Name e1) {

− // Catch block catches the exception thrown by try block

⚫ }

exceptions
⚫Types of exceptions:
− Synchronous: e.g. wrong input or data type errors, NaN
(Not a Number) divide by zero, etc.
− Asynchronous: e.g. Hard disk error, Keyboard
interrupts, system abort, Double faults, or errors
beyond control.
⚫ Important Information regarding exceptions
− Catch(…) can be used for all types of exceptions
− For primitive data types implicit conversion isnot
supported
exceptions
⚫ Important information associated with exceptions:
− If exception is thrown and not caught any catch
or adequate catch are not written, for example,
exception is generated on data type char but
catch is written in data type int, sich there is no
implicit data conversion supported the code will
abruptly terminate (or in such case catch(…) is
not written or event it also fails to catch the
exception) then program terminates abruptly.

exceptions
⚫ Important information associated with exceptions:
− In C++ compiler never checks an exception in functions
written by try has atleast one catch found. All catch may not
execute.
− try—catch block can be nested and can be re-thrown.
− When exception is thrown, all objects created in a respective
try block are destroyed before the control is transferred to the
catch block.
− Student to try example for each above.
− Simple exception Example
− Multiple catch Example

exceptions
⚫ Limitations of Exceptions
⚫ Exception may break the flow of the code since
multiple exit points due to catch exists, which also
makes code hard to read, interpret and debug.
⚫ Writing safe exception handling is challenging and
may lead to code leaks
⚫ In C++ there is no standard practice of writing
exceptions and multiple ways exists.
File Handling
⚫ Introduction
⚫ File Types
− Sequential access
− non-Sequential(Random) access or object oriented file
⚫ Class responsible for file handling or for file stream
operations: fstream, it includes two subclasses, viz;
(command #include <fstream>)
− ifstream: read from a file
− ostream: create, open and write to a file
File Handling
⚫ Classes for file stream operations
⚫ ios: Its a base class for all file handling sub classes/ derived classes and
related functions. This class has all the functionality used by all other derived
classes.
⚫ istream: This is derived class from ios related to the input stream. The
extraction operator (>>) is overloaded in this class to handle input streams
from file while program execution. Related functions are get(), getline(), read().
⚫ ostream: This is derived class from ios related to output stream. The insertion
operator (<<) is overloaded in this class to handle output streams to file during
program execution. Associated functions are put() and write().
⚫ streambuf: This class contains a pointer to a buffer which is used with input
and output streams.

File Handling
⚫ Classes for file stream operations
⚫ fstreambase: Handles stream operations. Base class for
fstream(formatted streams), ifstream and ofstream. This support open()
and close() functions.
⚫ ifstream: This class supports input operations with default input modes. It
supports open() function in default mode and also inherits, get(), getline(),
read(), seekg()(seek to get requested position), tellg().
⚫ ofstream: This class supports open() function in default output mode and
inherits put(), write(), seekp()(seek to position indicated), tellp().
⚫ fstream: This class supports simultaneous input,output operations streams
and inherits classes fro istream and ostream.
⚫ filebuf: It sets the file buffers for read and/or write. It also used for
computing length of a file.
File Handling
⚫ Opening and closing files
− Let MyFile be the object of class ofstream and let
“MyTestFile.txt” be the name and extension of a file.
− Command to create a file handle
⚫ std::ofsteam MyFile(“MyTestFile.txt”);
− Command to open a file opened
⚫ std::ofstream MyFile.open(“MyTestFile.txt”);

− Command to close a file


⚫ MyFile.close();
File Handling
⚫ Modes of files
⚫ Opening files using constructor
− ifstream (const* char filename,
ios_base::openmode mode=ios::in)
− ifstream fin(filename, openmode) default open
mode is ios::in
− ifstream fin(“filename”)
File Handling
⚫ Modes of files
⚫ Opening files using open method It calls default
constructor)
− ifstream fin;// get the file handle
− fin.open(“filename”, openmode);// use handle to
call open function
− fin.open(“filename”); // default read mode

File Handling
⚫ Modes ⚫
⚫ File open for reading in text
mode, internal stream buffer
⚫ ios::in ⚫ Input support input operations
⚫ ios::out ⚫ Output ⚫ File open for writing in text
⚫ ios::binary ⚫ Binary mode, internal stream buffer
support output to file writing
⚫ ios::ate ⚫ At end operations
⚫ ⚫ ⚫ File open in binary mode
⚫ Output to file starts at the
end of the file
File Handling
⚫ Modes ⚫

⚫ ios::app ⚫ Append
Append at the end of

⚫ ios::trunc ⚫ Truncate current content


⚫ ios::nocreat ⚫ Do not ⚫ The content existed before
e create opening a file is discarded
⚫ ios::norepla ⚫ Do not ⚫ Do not create a new file if
ce replace it does not exist
⚫ Do not replace existing file
with new file
File Handling
⚫ Default opening mode
− ifstream ios::in
− ofstream ios::out
− fstream ios::in | ios::out
⚫ Detecting end of file
− The eof() method of ios class is used to detect the end of file. The
error is raised on reaching the end of file.
− Syntax: bool eof() const;
⚫ Example Simple File Handling
⚫ Students to uncomment and correct the code

You might also like