Exception Handling Questions with Answers

14 practice questions on Exception Handling (C++) with answers and step-by-step solutions. Try 10 samples below, then practise all 14 free in the AptiNest app.

Sample questions

Question 1Medium

Pick out the correct Answer.

  1. Exceptions are not suitable for critical points in code.
  2. Exception are suitable for critical points in code.
  3. Both a & b
  4. none of the mentioned

Answer: A. Exceptions are not suitable for critical points in code.

Explanation

If there is many number of exceptions in the program means, We have to use multiple catch statement and it is hard to keep track of the program.

Step-by-step solution

  1. Exceptions add many catch blocks and make flow hard to follow.
  2. In time-critical code they are not suitable.
  3. So the statement that exceptions are not suitable is correct.
  4. Option A.

Why the other options are wrong

  • B. This is the opposite of the correct statement.
  • C. Only a is correct.
  • D. Option a is correct.

Tip: Use exceptions for rare errors, not for critical code paths.

Question 2Medium

What is the output of this program?

    #include <iostream>
    using namespace std;
    void empty() throw()
    {
        cout << "In empty()";
    }
    void with_type() throw(int)
    {
        cout << "Will throw an int";
        throw(1);
    }
    int main()
    {
        try
        {
            empty();
            with_type();
        }
        catch (int)
        {
            cout << "Caught an int";
        }
    }

(Compile as C++11; dynamic exception specifications were removed in C++17.)

  1. In empty()
  2. Will throw an int
  3. Caught an int
  4. All of the mentioned

Answer: D. All of the mentioned

Explanation

It will print all three because we are calling all functions in the main().

Step-by-step solution

  1. empty() prints 'In empty()' and does not throw.
  2. with_type() prints 'Will throw an int' and then throws 1.
  3. The catch(int) block prints 'Caught an int', so all three printed texts appear, which is option D.

Why the other options are wrong

  • A. This text is printed, but it is not the only output, so option A alone is incomplete.
  • B. This text is printed, but it is not the only output, so option B alone is incomplete.
  • C. This text is printed, but it is not the only output, so option C alone is incomplete.

Tip: Trace each function call in main() in order; when an exception is caught, the catch block output follows the throw.

Question 3Medium

Which handler is used to handle all types of exception?

  1. catch handler
  2. catch-all handler
  3. catch-none hendler
  4. none of the mentioned

Answer: B. catch-all handler

Explanation

A catch-all handler is used to handle all types of exceptions because it can catch any exception that is not caught by other specific handlers.

Step-by-step solution

  1. A catch-all handler catches every type of exception.
  2. It is used when no other specific handlers are present.
  3. This makes it suitable for handling all exceptions.
  4. Other options do not cover all exception types.
  5. Thus, the correct answer is catch-all handler.

Why the other options are wrong

  • A. A catch handler only catches specific exceptions.
  • C. A catch-none handler does not catch any exceptions.

Tip: Use a catch-all handler as a last resort to handle unexpected errors.

Question 4Medium

What is the output of this program?

    #include <iostream>
    #include <exception>
    using namespace std;
    void myunexpected ()
    {
        cout << "unexpected called\n";
        throw 0;
    }
    void myfunction () throw (int)
    {
        throw 'x';
    }
    int main ()
    {
        set_unexpected (myunexpected);
        try
        {
            myfunction();
        }
        catch (int)
        {
            cout << "caught int\n";
        }
        catch (...)
        {
            cout << "caught other exception\n";
        }
        return 0;
    }

(Compile as C++11; dynamic exception specifications were removed in C++17.)

  1. caught other exception
  2. caught int
  3. unexpected called
  4. both caught int and unexpected called

Answer: D. both caught int and unexpected called

Explanation

myfunction throws 'x', which the throw(int) spec forbids, so myunexpected runs and prints 'unexpected called'. It throws 0, an int, which is allowed, so 'caught int' is printed as well.

Step-by-step solution

  1. myfunction() has the spec throw(int) but throws the char 'x', which breaks the spec.
  2. The runtime then calls the unexpected handler, myunexpected.
  3. myunexpected prints 'unexpected called' and throws 0, an int.
  4. An int is allowed by throw(int), so it leaves myfunction and reaches the try block.
  5. catch (int) matches, so 'caught int' is printed and catch (...) is skipped. Both lines print: option D.

Why the other options are wrong

  • A. The int is caught by catch (int) first, so 'caught other exception' never prints.
  • B. Misses the 'unexpected called' line printed by the handler before the int is thrown.
  • C. Misses the 'caught int' line, because the thrown int is caught.

Tip: A wrong type thrown under a dynamic exception spec goes to unexpected(); whatever the handler throws must also match the spec.

Question 5Medium

Where does the exception are handled?

  1. inside the program
  2. outside the regular code
  3. both a & b
  4. none of the mentioned

Answer: B. outside the regular code

Explanation

Exceptions provide a way to react to exceptional circumstances (like runtime errors) in programs by transferring control to special functions called handlers.

Step-by-step solution

  1. Exceptions are raised when something unusual happens during the program.
  2. Control moves to special functions called handlers, which are kept apart from the normal code path.
  3. So exceptions are handled outside the regular code, and the correct answer is option B.

Why the other options are wrong

  • A. Handlers are not part of the normal flow; they run only when an exception is thrown.
  • C. Option B is correct, so both is wrong.
  • D. Option B is correct, so none is wrong.

Tip: Try holds the normal code; catch holds the handler.

Question 6Medium

Identify the correct statement about throw(type).

  1. A function can throw any type of exceptions.
  2. A function can throw an exception of certain type only.
  3. A function cant throw any type of exception.
  4. none of the mentioned

Answer: B. A function can throw an exception of certain type only.

Explanation

When the throw keyword is written by itself, it is a way of asking the compiler to send the exception to another handler.
In fact, if there is no other handler written by you, the processing would be handed to the operating system. In this case, if you run the program and type a negative integer, since the program is not prepared to handle the exception itself, because of the presence of a single throw, the operating system would take over and display its own message.
This would be "abnormal program termination" on a Microsoft Windows operating system.

Step-by-step solution

  1. A dynamic exception specification such as throw(type) lists the exception types a function may throw.
  2. A function written with throw(int) can throw an exception of that certain type only.
  3. So the correct statement is option B.

Why the other options are wrong

  • A. A function with an exception specification is not allowed to throw any type.
  • C. A function with throw(type) can throw the listed type, so it is not barred from throwing.
  • D. Option B is correct, so 'none of the mentioned' is wrong.

Tip: An exception specification limits which exception types a function may throw; an empty list means none.

Question 7Medium

What is the output of this program?

    #include <iostream>
    using namespace std;
    int main()
    {
        int x = -1;
        char *ptr;
        ptr = new char[256];
        try
        {
            if (x < 0)
            {
                throw x;
            }
            if (ptr == NULL)
            {
                throw " ptr is NULL ";
            }
        }
        catch (...)
        {
            cout << "Exception occurred: exiting "<< endl;
        }
        return 0;
    }
  1. -1
  2. ptr is NULL
  3. Exception occured: exiting
  4. none of the mentioned

Answer: C. Exception occured: exiting

Explanation

catch(...) is used to catch all types of exceptions arising in the program.
Output:
Exception occured: exiting

Step-by-step solution

  1. X is -1, so x < 0 is true and the code throws the int -1.
  2. catch(...) catches any exception type, including an int.
  3. The catch block prints the fixed text 'Exception occurred: exiting' and then a newline.
  4. The string throw is never reached, so the answer is option C; the option is spelled 'occured'.

Why the other options are wrong

  • A. The value -1 is thrown and caught, but the catch block prints fixed text, not -1.
  • B. The string throw is never reached because the int throw happens first.
  • D. Option C matches the printed text, so none is wrong.

Tip: catch(...) catches every exception type; the first throw that is reached is the one handled.

Question 8Medium

What will happen if an exception that is thrown may causes a whole load of objects to go out of scope?

  1. Terminate the program
  2. Produce a runtime error
  3. It will be added to the overhead
  4. None of the mentioned

Answer: C. It will be added to the overhead

Explanation

If exception is occur and we does not handle that exception.
It may be chances to crash the program and it terminate abnormally.
it may be possibility to damage a hardware also.
But if we handle the situation through try catch block the programme run without any overheads.

Step-by-step solution

  1. The question says a thrown exception can take many objects out of scope.
  2. Leaving scope destroys those objects, which is extra work done during the throw.
  3. That extra work is the overhead that option C names.
  4. So the correct answer is option C, it will be added to the overhead.

Why the other options are wrong

  • A. The program ends only when the exception is not caught.
  • B. No runtime error is described; the question is about objects leaving scope.
  • D. Option C does describe the result, so none is not right.

Tip: Exceptions unwind the stack and destroy local objects; keep code on the throw path light.

Question 9Medium

Where does the standard exception classes are grouped?

  1. namespace std
  2. error
  3. catch
  4. none of the mentioned

Answer: A. namespace std

Explanation

As these are standard exceptions, they need to be defined in the standard block, So it is defined under namespace std.

Step-by-step solution

  1. The C++ standard exception classes such as std::exception are declared in the std namespace.
  2. So the standard exception classes are grouped in namespace std.
  3. So the answer is namespace std.

Why the other options are wrong

  • B. Error is not the name of the group that holds the standard exceptions.
  • C. Catch is a keyword for handling exceptions, not a grouping.
  • D. Namespace std is correct, so none of the mentioned is wrong.

Tip: Standard library names such as exception live in namespace std.

Question 10Medium

How many parameters does the throw expression can have?

  1. 1
  2. 2
  3. 3
  4. 4

Answer: A. 1

Explanation

In c++ program, We can be able to throw only one error at a time.

Step-by-step solution

  1. Throw expression in C++ can only throw one value at a time.
  2. It is not possible to throw multiple values using a single throw expression.
  3. The throw keyword is used with a single expression as its parameter.
  4. Therefore, the throw expression can have only 1 parameter.
  5. Final answer: 1.

Why the other options are wrong

  • C. Throwing three values is not allowed in C++.
  • D. Throwing four values is not supported by the language.

Tip: Remember: Only one exception can be thrown per throw expression.

Practise all 14 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C++ topics · All topics