Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I add exception handling to this code? //Include Header files #include #i

ID: 3871738 • Letter: H

Question

How do I add exception handling to this code?

//Include Header files #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; //Main program code int main() { //Opens the output file ofstream outputFile; //opens the output file outputFile.open ("outputFile.txt"); //declare variables int NUmberOfQuestions; string options, typeOf, Ques, Ans; int totalOptions; //gets the number of questions cout << "Enter total number of question: "; cin >> NUmberOfQuestions; //enters into the output file outputFile << NUmberOfQuestions << endl; // for loop for (int i = 0; i < NUmberOfQuestions; ++i) { //prints the questions cout << " Question " << (i+1) <> typeOf; //checks the question type if(typeOf == "TF") { //ignore case cin.ignore(); //Enters the question cout << "Enter question: "; getline(cin,Ques); //enters the question cout << "Enter Ans(True/False): "; cin >> Ans; //Enters into outfile outputFile << typeOf << endl; //Enters into outfile outputFile << Ques << endl << Ans << endl; } //checks the question type if(typeOf == "MC") { cin.ignore(); //Enters the question cout << "Enter Question: "; getline(cin,Ques); //Enters the question cout << "Enter number of options: "; cin >> totalOptions; //Enters into outfile outputFile << typeOf << endl; //Enters into outfile outputFile << Ques << endl; //for loop for (int j = 0; j < totalOptions; ++j) { cin.ignore(); //Enter the options cout << "Enter options " << (j+1) <<": "; getline(cin , options); //Includes in the output file outputFile << options << endl; } //checks the correct answer cout << "Enter answer(correct options): "; cin >> Ans; //output file outputFile << Ans << endl; } } //closes the file outputFile.close(); return 0; }

Explanation / Answer

**Sample Exception handling program in C++

#include <iostream>

using namespace std;

int main()

{

    try {

// The doubtful code is written in the try block

// Throwing an exception as an integer

       throw 20;

    }

// If any statement is throwing an exception as an integer then this catch block is executed

    catch (int x) {

        cout << "Caught " << x;

    }

// If no exception is caught by a catch block then this default catch block is executed

    catch (...) {

        cout << "Default Exception ";

    }

    return 0;

}

**For your program you didnot mention what type of exception you want to raise so i am just implementing some sample catch blocks in your code

# include <stdio.h>
# include <stdlib.h>

//Main program code
int main() {
try{
//Opens the output
file ofstream outputFile;
//opens the output
file outputFile.open ("outputFile.txt");
throw 20
//declare variables
int NUmberOfQuestions;
string options, typeOf, Ques, Ans;
int totalOptions;
//gets the number of questions
cout << "Enter total number of question: ";
cin >> NUmberOfQuestions;
//enters into the output
file outputFile << NUmberOfQuestions << endl;
// for loop
for (int i = 0; i < NUmberOfQuestions; ++i) {
//prints the questions
cout << " Question " << (i+1) <> typeOf;
//checks the question type
if(typeOf == "TF") {
//ignore case
cin.ignore();
//Enters the question
cout << "Enter question: "; getline(cin,Ques);
//enters the question
cout << "Enter Ans(True/False): ";
cin >> Ans;
//Enters into outfile
outputFile << typeOf << endl;
//Enters into outfile
outputFile << Ques << endl << Ans << endl;
}
//checks the question type
if(typeOf == "MC") {
cin.ignore();
//Enters the question
cout << "Enter Question: "; getline(cin,Ques);
//Enters the question
cout << "Enter number of options: ";
cin >> totalOptions;
//Enters into outfile
outputFile << typeOf << endl;
//Enters into outfile
outputFile << Ques << endl;
//for loop
for (int j = 0; j < totalOptions; ++j)
{
cin.ignore();
//Enter the options
cout << "Enter options " << (j+1) <<": ";
getline(cin , options);
//Includes in the output file
outputFile << options << endl;
}
//checks the correct answer
cout << "Enter answer(correct options): ";
cin >> Ans;
//output file
outputFile << Ans << endl;
}
} //closes the file
}
catch(int x){
if(x == 20)
cout<<" Error in reading the file";
}
catch(...){
cout<<"Unknown Exception caught";
}
outputFile.close();
return 0;
}

**Comment for any further help or queries

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote