Problem 2
What is the difference between a try block and a catch block?
Problem 3
What will happen if an exception is thrown but not caught?
Problem 4
What happens if no exception is thrown in a try block?
Problem 5
What happens if an exception is thrown in a try block?
Problem 6
What is wrong with the following \(\mathrm{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; }
Problem 7
What is wrong with the following \(\mathrm{C}++\) code? Also, provide the correct code. double radius; try { cout << "Enter the radius: "; cin >> radius; cout << endl; if (radius < 0.0) throw radius; cout << "Area: " << 3.1416 * radius * radius << endl; } cout << "Entering the catch block." << endl; catch (double x) { cout << "Negative radius: " << x << endl; }
Problem 8
Consider the following \(\mathrm{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;} a. In this code, identify the try block. b. In this code, identify the catch block. c. In this code, identify the catch block parameter and its type. d. In this code, identify the throw statement.
Problem 11
Consider the following \(\mathrm{C}++\) code: int lowerLimit; int divisor; int result; try { cout << "Entering the try block." << endl; if (divisor == 0) throw 0; if (lowerLimit < 100) throw string("Lower limit violation."); result = lowerLimit / divisor; cout << "Exiting the try block." << endl; } catch (int x) { cout << "Exception: " << x << endl; result = 120; } catch (string str) { cout << "Exception: " << str << endl; } cout << "After the catch block" << endl; What is the output if: a. The value of lowerLimit is \(50,\) and the value of divisor is \(10 ?\) b. The value of lowerLimit is \(50,\) and the value of divisor is \(0 ?\) c. The value of lowerLimit is \(150,\) and the value of divisor is \(10 ?\) d. The value of lowerLimit is \(150,\) and the value of divisor is 0 ?
Problem 12
If you define your own exception class, what is typically included in that class?
Problem 13
What type of statement is used to rethrow an exception?