CARVIEW |
Exception Handling
Exception Handling Quiz
Question 1
Which of the following is the correct syntax to handle exceptions in C++?
try { ... } catch { ... }
try { ... } catch(Exception e) { ... }
try { ... } throw(Exception e) { ... }
catch { ... } try { ... }
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?
#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
Question 6
Consider this code:
#include <iostream>
using namespace std;
class MyException : public exception {
public:
const char* what() const noexcept override {
return "Custom Exception";
}
};
int main() {
try {
throw MyException();
}
catch (exception& e) {
cout << e.what();
}
}
Custom Exception
No Output
MyException
Compilation Error
There are 6 questions to complete.