Question # 1 What is the difference between a try block and a catch block? Quest
ID: 3567844 • Letter: Q
Question
Question # 1 What is the difference between a try block and a catch block?
Question # 2 What will happen if an exception is thrown but not caught?
Question # 3 Consider the following C++ code:
int lowerLimit; . . .
try
{
}
catch (exception eObj) {
cout << "Exception: " << eObj.what() << endl;
}
cout << "After the catch block" << endl;
What is the output if:
a. The value of lowerLimit is 50?
b. The value of lowerLimit is 150?
Question # 4 Define an exception class called tornadoException. The class should have two constructors, including the default constructor. If the exception is thrown with the default constructor, the method what should return "Tornado: Take cover immediately!". The other constructor has a single parameter, say, m, of the int type. If the exception is thrown with this constructor, the method what should return "Tornado: m miles away; and approaching!"
Explanation / Answer
1)
The statements that may generate an exception are placed in a try block. The try block also contains statements that should not be executed if an exception occurs. A block specifies the type of exception it can catch and contains and exception handler.
2)
If an exception is thrown, but not caught, program execution will be terminated without fulfilling user requirements.
3)
Exception: Lower limit violation.
After the catch block
2. If lowerLimit = 150, there will be no exception. So the output is
Exiting the try block.
After the catch block