Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need some help with the following problems, thanks in advance: A) What is the

ID: 3556247 • Letter: I

Question

I need some help with the following problems, thanks in advance:

A) What is the difference between try block and catch block?

B) What happens if no exception is thrown in a try block?

C) What is wrong with the following C++ code? Also, provide the correct code.
double balance = 25000;
double intRate;

catch (double x)
{
      cout << "Negative interest rate: " << x << endl;
}
try
{
      cout << "Enter the interest rate: ";
      cin >> intRate;
      cout << endl;

      if (intRate < 0.0)
            throw intRate;

      cout << "Interest: $" << balance * intRate / 100 << endl;
}

D) Consider the following C++ code:
double balance;

try
{
      cout << "Enter the balance: ";
      cin >> balance;
      cout << endl;

      if (balance < 1000.00)
            throw balance;

      cout << "Leaving the try block." << endl;
}
catch (double x)
{
      cout << "Current balance: " << x << endl
            << "Balance must be greater than 1000.00" << endl;
}

    1. In this code, identify the try block.
    2. In this code, identify the catch block.
    3. In this code, identify the catch block parameter and its type.
    4. In this code, identify the throw statement.

E) Consider the following C++ code:
int lowerLimit;
.
.
.
try
{
      cout << "Entering the try block." << endl;

      if (lowerLimit < 100)
            throw exception("Lower limit violation.");

      cout << "Exiting the try block." << endl;
}
catch (exception eObj)
{
      cout << "Exception: " << eObj.what() << endl;
}
cout << "After the catch block" << endl;

What is the output if:
    1. The value of lowerLimit is 50?
    2. The value of lowerLimit is 150?

Explanation / Answer

Dear,

A)

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.

B)

If an exception is thrown, but not caught, program execution will be terminated without fulfilling user requirements.

C)

Above C++ code has given wrong order of statements.

try block followed by catch blocks is the correct format but in above program catch block followed by try block.

Three errors generated:

: no try block associated with this catch handler

: 'x' : undeclared identifier

: 'try' block starting on line '20' has no catch handlers

Corrected code: just change the order of try and catch blocks.

      double balance=25000;

     double intRate;

     try

     {

          cout<<"Enter Interest rate";

          cin>>intRate;

          cout<<endl;

          if(intRate<0.0)

              throw intRate;

          cout<<"Interest: $"<<balance * intRate/100<<endl;

     }

     catch(double x)

     {

          cout<<"Negative interest rate :"<<x<<endl;

     }

D)

1.
try

{

cout <<