Exceptions and file handling
Exceptions and file handling
⚫ }
⚫ Catch (Exception Name e1) {
⚫ }
⚫
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”);
⚫ ios::app ⚫ Append
Append at the end of
⚫