Question # 1 What is the difference between a try block and a catch block? Quest
ID: 3567845 • 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!"
Question # 5
Briefly explain three exception-handling techniques
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 catch 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)
a)
If lowerLimit = 50, the exception will be thrown. so the output is
Exception: Lower limit violation.
After the catch block
b)
If lowerLimit = 150, there will be no exception. So the output is
Exiting the try block.
After the catch block
------------------------------------------------------------------------------------------------------------------------------------------
4)
#include <iostream>
#include <string>
using namespace std;
class tornadoException
{
public:
tornadoException()
{
message = "Tornado: Take cover immediately";
}
tornadoException(int m)
{
message = "Tornado: miles away; and approaching!";
}
string what()
{
return message;
}
private:
string message;
};
int main()
{
int miles;
try {
cout << "Enter miles";
cin >> miles;
cout << endl;
if (miles <= 0) {
throw tornadoException();
}
else {
throw tornadoException(miles);
}
}
catch (tornadoException e){
cout << "In tornadoException " << endl
<< e.what() << endl;
}
return 0;
}
Sample Output:
Enter miles4
In tornadoException
Tornado: 4 miles away; and approaching!
---------------------------------------------------------------------------------------------------------------------------
5)
a. Terminate the programme
b. Fix the error and continue
c. Log the error and continue