C++ Exception Handling and file handling

 

C++ Exceptions

When executing C++ code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things.

 

When an error occurs, C++ will normally stop and generate an error message. The technical term for this is: C++ will throw an exception (throw an error).

 

C++ try and catch

Exception handling in C++ consist of three keywords: try, throw and catch:

 

The try statement allows you to define a block of code to be tested for errors while it is being executed.

 

The throw keyword throws an exception when a problem is detected, which lets us create a custom error.

 

The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

 

The try and catch keywords come in pairs:

 

 

try {

  // Block of code to try

  throw exception; // Throw an exception when a problem arise

}

catch () {

  // Block of code to handle errors

}

 

 

#include <iostream>

using namespace std;

 

int main() {

  try {

    int age = 15;

    if (age >= 18) {

      cout << "Access granted - you are old enough.";

    } else {

      throw (age);

    }

  }

  catch (int myNum) {

    cout << "Access denied - You must be at least 18 years old.\n";

    cout << "Age is: " << myNum; 

  }

  return 0;

}

 

 

C++ Files

 

The fstream library allows us to work with files.

 

To use the fstream library, include both the standard <iostream> AND the <fstream> header file:

 

There are three classes included in the fstream library, which are used to create, write or read files:

 

Class       Description

 

ofstream          Creates and writes to files

ifstream           Reads from files

fstream              A combination of ofstream and ifstream: creates, reads, and writes to files

 

To create a file, use either the ofstream or fstream class, and specify the name of the file.

 

To write to the file, use the insertion operator (<<).

 

#include <iostream>

#include <fstream>

using namespace std;

 

int main() {

  ofstream MyFile("filename.txt");

 

  MyFile << "Files can be tricky, but it is fun enough!";

 

  MyFile.close();

}

 

 

Read a File

 

To read from a file, use either the ifstream or fstream class, and the name of the file.

 

Note that we also use a while loop together with the getline() function (which belongs to the ifstream class) to read the file line by line,

and to print the content of the file:

 

 

 

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

 

int main () {

  ofstream MyWriteFile("filename.txt");

 

  MyWriteFile << "Files can be tricky, but it is fun enough!";

 

  MyWriteFile.close();

 

  string myText;

 

  ifstream MyReadFile("filename.txt");

 

  while (getline (MyReadFile, myText)) {

    cout << myText;

  }

 

  MyReadFile.close();

}

 

 

 

 

 

 

 

 

 

 

Comments

  1. If you're looking for cookery assignment help Australia, choosing expert academic assistance can make challenging culinary assignments much easier. Professional writers with knowledge of hospitality and cookery courses provide well-researched, plagiarism-free solutions that align with Australian academic standards. Whether you need help with food safety, menu planning, nutrition, or kitchen operations, reliable assignment support ensures timely submissions and quality work. The right guidance not only saves time but also helps you understand key concepts and improve your academic performance.

    ReplyDelete

Post a Comment

Popular posts from this blog

Sorting DSA in c++

Basic DSA programs in c++