C++ Exception Handling

Last Updated :
Discuss
Comments

Question 1

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

  • A

    ofstream file("data.txt");

  • B

    ifstream file("data.txt");

  • C

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

  • D

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

Question 2

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

  • A

    Program skips the exception


  • B

     Program terminates using std::terminate()

  • C

    It is silently ignored

  • D

     Control goes back to the main function

Question 3

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

  • A

    std::runtime_error

  • B

    std::bad_alloc

  • C

    std::overflow_error

  • D

    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";
}
}


  • A

    Char Exception

  • B

    Default Handler

  • C

    Compilation Error

  • D

    No Output

Question 5

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


  • A

    retry

  • B

    throw

  • C

    catch

  • D

    throw e

There are 5 questions to complete.

Take a part in the ongoing discussion