10 Exception Handling And File I O

Object Oriented Programming · Unit 10

Exception Handling and File I/O

Exam-focused notes for Exception Handling and File I/O (Object Oriented Programming, BIT153): what the TU syllabus asks and how it has actually been tested, with 15 solved past questions from this unit.

What this unit covers

  • Exception handling definition and purpose
  • Try, catch, and throw statements
  • Types of exceptions
  • Exception handling examples
  • Stream definition
  • Text file reading and writing
  • Binary file reading and writing
  • File handling with objects
  • Namespace definition
  • Stream manipulators: setw and setprecision

Exception handling definition and purpose

20825 marks

Write a program to handle division by zero exception using try, catch, and throw. [5]

In C++, exception handling uses three keywords: - try - block of code that may throw an exception - throw - used to throw/raise an exception when an error occurs - catch - block that handles the thrown exception --- --- Case 1: Normal Division Case 2: Divis...

Full solved answer →
20815 marks

Why do we need to handle exception? Illustrate with an example. [5]

An exception is an abnormal event detected while a program is running, such as a file that will not open, a request for memory that cannot be met, or an argument that makes an operation meaningless. In C++ the code that detects the trouble signals it with t...

Full solved answer →
20805 marks

What is exception handling? Explain types of exception handling and explain with suitable example. [5]

Exception handling is the mechanism by which a C++ program reports a fault at the point where it is detected and deals with it at the point where there is enough context to decide what to do. The detecting code executes throw, which creates an exception obj...

Full solved answer →
20785 marks

What is exception handling? Explain how to handle an exception with appropriate example. [5]

Exception handling is the mechanism by which a program signals a fault at the place where it is discovered and deals with it at the place that has enough context to decide what to do. An exception is an unexpected condition that arises while the program run...

Full solved answer →
05 marks

Write a program to handle "array index out of range" exception. [5]

A raw array subscript in C++ is not checked. Writing numbers[10] on an array of five elements does not throw anything and does not report an error; it simply reads or writes memory that does not belong to the array, which is undefined behaviour. There is no...

Full solved answer →

Text file reading and writing

20825 marks

Write a C++ program to read a text file named data.txt and display its contents on the screen. Also, count the number of lines, words, and characters in the file and display these counts after showing the file content. [5]

Suppose data.txt contains: Program Output: Concept Description ------ ifstream Used to open and read from a file getline() Reads one complete line at a time inFile check Verifies file was opened successfully line.length() Returns number of characters in a l...

Full solved answer →
20795 marks

Define stream. How do you read the text from file? Describe. [5]

A stream is an abstraction for a flow of characters between a program and some source or destination of data, such as a file, the keyboard, the screen, or a block of memory. The program deals only with the stream, and the library deals with the device behin...

Full solved answer →
20785 marks

Write a program that writes the contents of file 1 into file 2. [5]

--- Step Function Used Purpose ----------------------------- Open file1 fopen("file1.txt", "r") Opens file1 in read mode Open file2 fopen("file2.txt", "w") Opens file2 in write mode (creates if not exists) Read character fgetc(file1) Reads one character at ...

Full solved answer →
05 marks

Write a program to read text from keyboard and write it to a file named "test.txt". Again read the content of file and display it in the monitor. [5]

1. Read text input from the keyboard 2. Write the text to a file named "test.txt" 3. Read back the content from "test.txt" 4. Display the content on the monitor --- --- --- Function Purpose ------------------- fopen("test.txt", "w") Opens file for writing (...

Full solved answer →

Binary file reading and writing

208110 marks

Define stream.How do you read and write binary files?Write a program to read an integer from the user and if the integer is less than 100 then write it in file named "SMALL.TXT" otherwise write in file named "LARGE.TXT".[2+3+5]

--- A stream is an abstraction that represents a flow of data between a program and an input/output device (such as a keyboard, monitor, disk file, etc.). - A stream acts as a logical interface between the program and the actual I/O device. - In C++, stream...

Full solved answer →
2080.15 marks

Write a program that writes objects to a binary file and reads objects from the same file. [5]

Binary file I/O in C++ allows storing objects (structured data) directly in binary format using fstream. The key functions used are: - write() - writes raw bytes of an object to a binary file - read() - reads raw bytes from a binary file back into an object...

Full solved answer →

Stream manipulators

20815 marks

Illustrate the use of set w and set precision with example. [5]

setw and setprecision are output formatting manipulators defined in the header file <iomanip. They are used to control the appearance of output in C++. --- - Sets the minimum field width for the next output value. - The output is right-aligned by default wi...

Full solved answer →

File handling with objects

20805 marks

Write a Program to read and write values through object using File Handling. [5]

File handling in C++ allows storing and retrieving object data (attributes) to/from files using fstream. We use write() to store binary data and read() to retrieve it. --- --- --- Feature Description ------ fstream Used for both reading and writing ios::out...

Full solved answer →

Namespace definition

20795 marks

Define namespace. List any two manipulators with their uses. [5]

A namespace is a declarative region that provides a scope to the identifiers (names of types, functions, variables, etc.) inside it. It is used to organize code into logical groups and to prevent name collisions that can occur especially when a codebase inc...

Full solved answer →

Try, catch, and throw statements

2080.15 marks

Illustrate Exception handling in C++ using try, throw and catch statements. [5]

Exception handling is a mechanism in C++ that allows a program to detect and handle runtime errors (exceptions) gracefully, without crashing the program. C++ provides three keywords for exception handling: Keyword Purpose ------------------ try Encloses the...

Full solved answer →