Question 51
MediumIn C++, what happens if an exception is thrown and never caught?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
In C++, what happens if an exception is thrown and never caught?
It is ignored
std::terminate is called and the program ends
The exception is printed and the program continues
The function returns 0
Sign up free to see the answer and a step-by-step explanation.
What does 'noexcept' on a C++ function mean?
The function returns an error code
The function promises not to throw; if it does, the program terminates
The function catches all exceptions
The function cannot be called
Sign up free to see the answer and a step-by-step explanation.
What is stack unwinding in C++?
Destroying local objects of each function as an exception propagates out of it
Undoing a database transaction
Removing elements from a stack container
Freeing heap memory automatically
Sign up free to see the answer and a step-by-step explanation.
In Python, what is the else clause of a try statement for?
Code that runs on error
Code that should run only if no exception was raised in the try block
Code that replaces the except block
Code that always runs
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
try:
try:
raise KeyError
except KeyError:
print("Inner", end=" ")
raise
print("Done")
except KeyError:
print("Outer")
Inner Outer
Inner
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
struct Base {};
struct Derived : Base {};
int main() {
try {
throw Derived();
} catch (Base&) {
cout << "base";
} catch (Derived&) {
cout << "derived";
}
return 0;
}
derived
base
Sign up free to see the answer and a step-by-step explanation.
Why should destructors in C++ not throw exceptions?
The compiler forbids throw anywhere in a class
Throwing is slower in destructors
If a destructor throws during stack unwinding from another exception, std::terminate is called
Destructors can't contain code
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
#include <stdexcept>
using namespace std;
int main() {
try {
cout << "1 ";
throw runtime_error("x");
cout << "2 ";
} catch (const exception&) {
}
cout << "3";
return 0;
}
1 2 3
1
Sign up free to see the answer and a step-by-step explanation.
What is exception chaining?
Wrapping a caught exception as the cause of a new, higher-level exception
Throwing the same exception twice
Catching many exceptions in one block
Ignoring exceptions
Sign up free to see the answer and a step-by-step explanation.
Which is the best practice when catching exceptions?
Use exceptions for normal control flow
Wrap every line in its own try block
Catch specific exceptions you can handle and let others propagate
Always catch Exception and ignore it
Sign up free to see the answer and a step-by-step explanation.
Outer
Inner Outer Done
Terminate
base derived
3
1 3