C++ Exception Handling

Last Updated :
Discuss
Comments

Question 1

Which of the following is used to open a file for reading in C++?

  • ofstream file("data.txt");

  • ifstream file("data.txt");

  • fstream file("data.txt", ios::out);

  • file.open("data.txt", ios::write);

Question 2

What happens if an exception is thrown but no catch block matches it?

  • Program skips the exception


  •  Program terminates using std::terminate()

  • It is silently ignored

  •  Control goes back to the main function

Question 3

Which standard exception class is thrown when memory allocation with new fails?

  • std::runtime_error

  • std::bad_alloc

  • std::overflow_error

  • std::invalid_argument

Question 4

What is the output of the following code?

C++
#include <iostream>
using namespace std;
int main() {
try {
throw 10;
}
catch (char e) {
cout << "Char Exception";
}
catch (...) {
cout << "Default Handler";
}
}


  • Char Exception

  • Default Handler

  • Compilation Error

  • No Output

Question 5

 Which keyword is used to rethrow the currently handled exception inside a catch block?


  • retry

  • throw

  • catch

  • throw e

There are 5 questions to complete.

Take a part in the ongoing discussion