Problem Solving with C++ plus MyProgrammingLab with Pearson eText-- Access Card Package (9th Edition)
9th Edition
ISBN: 9780133862218
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 16.1, Problem 4STE
What happens when a throw statement is executed? This is a general question. Tell what happens in general, not simply what happens in the code in Self-Test Question 1 or some other sample code.
1. What output is produced by the following code?
int waitTime = 46; try { cout << "Try block entered.\n"; if (waitTime > 30) throw waitTime; cout << "Leaving try block.\n"; } catch(int thrownValue) { cout << "Exception thrown with\n" << "waitTime equal to " << thrownValue << endl; } cout << "After catch block." << endl; |
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Solve In Python
Provide Screenshots of Input and Ouput
For this lab, you will modify your Lab 7 to make several improvements to the getter/settermethods. Instead of having them return True/False, modify each one to throw an exception ifinvalid data is passed.Modify these methods in the following way:• add_hours()o If the number of hours being added is less than 0, throw an exception• add_sales()o If the amount of sales being added is less than 0, throw an exception• set_employee_number()o The employee number must be an integer. If the given input is not an integer,throw an exception• set_office_number()o If the office number given is less than 100 or greater than 500, throw anexception.• set_name()o If the given name is empty, throw an exceptiono Any of the following characters should be removed from the name: ‘_’, ‘.’, ‘-‘ (Underscore, period, and dash)• set_birthdate()o If the given value for the month is less than 1 or greater than 12, throw anexceptiono If the given value for the…
I have a question about the following code, why do they have different output, please explain to me.
class Test
{
private:
int x;
int y;
public:
Test(int x = 0, int y = 0) { this->x = x; this->y = y; }
Test &setX(int a) { x = a; return *this; }
Test &setY(int b) { y = b; return *this; }
void print() { cout << "x = " << x << " y = " << y << endl; }
};
int main()
{
Test obj1(5, 5);
// Chained function calls. All calls modify the same object
// as the same object is returned by reference
obj1.setX(10).setY(20);
obj1.print();
return 0;
}
#include<iostream>
using namespace std;
class Test
{
private:
int x;
int y;
public:
Test (int x = 0, int y = 0) { this->x = x; this->y = y; }
Test setX(int a) { x = a; return *this; }
Test setY(int b) { y = b; return *this; }
void print() { cout << "x = " << x << " y = " << y << endl; }
};
int main()
{
Test obj1;…
What is the difference between using @Test(expected = TypeOfException) and using try/catch when testing if a method or constructor properly returns an exception?
A. They do the same thing.
B. @Test(expected = ...) is the proper way to unit test in java, and try/catch should only be used when unit testing in Python.
C. @Test(expected = ...) will give a passing test as soon as one exception of the proper type is thrown and then stop the test, but using try/catch will allow you to test many examples in the same test and will only stop after running all examples or reaching a fail() or an assert statement that fails.
D. We should never use try/catch when unit testing, otherwise we will catch the exception and then won't know if it is actually thrown.
Chapter 16 Solutions
Problem Solving with C++ plus MyProgrammingLab with Pearson eText-- Access Card Package (9th Edition)
Ch. 16.1 - Prob. 1STECh. 16.1 - What would be the output produced by the code in...Ch. 16.1 - Prob. 3STECh. 16.1 - What happens when a throw statement is executed?...Ch. 16.1 - In the code given in Self-Test Exercise 1, what is...Ch. 16.1 - Prob. 6STECh. 16.1 - Prob. 7STECh. 16.1 - What is the output produced by the following...Ch. 16.1 - What is the output produced by the program in...Ch. 16.2 - Prob. 10STE
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- 4.) How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j <= 5; j++) System.out.println(i * j) a. 100 b. 60 c. 50 d. 45 5.) Will the following program terminate? int balance = 10; while (true) { if (balance < 9) break; balance = balance - 9; } a.Yes b. No 6.) What exception type does the following program throw? public class Test { public static void main(String[] args) { System.out.println(1 / 0); } } a. ArithmeticException b. ArrayIndexOutOfBoundsException c. StringIndexOutOfBoundsException d. ClassCastException e. No exceptionarrow_forwardComplete both Unit Tests using the given code Unit Test #1 performOperation addition test Build Output NtTest6b116967.java:9: error: performOperation(double,double,String) has private access in Calculator assertTrue(tester201.performOperation(5, 2, "+") == 7.00); ^ Test Contents Calculator tester201 = new Calculator(); @Test public void unitTest() { assertTrue(tester201.performOperation(5, 2, "+") == 7.00); } Unit Test #2 performOperation multiplication test Build Output NtTeste16b3fd1.java:9: error: performOperation(double,double,String) has private access in Calculator assertTrue(tester202.performOperation(5, 2, "*") == 10); Test Contents ^ Calculator tester202 = new Calculator(); @Test public void unitTest() { assertTrue(tester202.performOperation(5, 2, "*") == 10); } The Given Code // Calculator.java - This program performs arithmetic, ( +. -, *. /, % ) on two…arrow_forwardJava Code: -Creates ProgramNode, loops over Parse calls correctly, throws exception if unknown item found -(ParseFunction) Uses MatchAndRemove(), handles any number of parameters, fills in ASTNode correctly -(ParseAction) Uses MatchAndRemove(), handles any number of parameters, fills in ASTNode correctly Make sure to give the correct code for parser.java with the screenshots of the output. There must be test cases involved, so make sure to have test cases as well.arrow_forward
- trace the following code and give the output. for (int k=5; k<=20; k+=5) cout << "? ";arrow_forwardThis is a debugging question - The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. Code I fixed but seem to be missing a check, I will add a screenshot - // Catch exceptions for array index out of bounds // or dividing by 0 import java.util.*; public class DebugTwelve3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String inStr; int num, result; int[] array = {12, 4, 6, 8}; System.out.println("Enter a number "); inStr = input.nextLine(); try { num = Integer.parseInt(inStr); for (int x = 0; x < array.length; ++x) { result = array[x] / num; System.out.println("Result of division is " + result); } result = array[num];…arrow_forwardWhat is the output of the following code? for (int k = 2; k <= 10000; k = k * k)cout << k << " ";cout << endl;arrow_forward
- Consider the following code: public static void test_b(int n) { if (n>0) test_b(n-2); System.out.println(n + " "); } What is printed by the call test_b(6)?arrow_forwardWhat is the output of the following code fragment? int n = 1; while (n <= 5) cout << n << ' '; n++;arrow_forwardConsider the following code and write the output of this code? int **p; p = new int* [5];for (int i = 0; i < 5; i++) p[i] = new int[3]; for (int i = 1; i < 5; i++) for (int j = 0; j < 3; j++) p[i][j] = 2 * i + j; for (int i = 1; i < 5; i++) { for (int j = 0; j < 3; j++) cout << p[i][j] << " "; cout << endl;}arrow_forward
- C++Write a program SEGMENT that does the following: Asks the user for his/her birthday using 2 integer values, i.e., int month, int year. Using a try/catch block with two catch blocks, check for the following exceptions: Invalid month (i.e., a month less than or equal to 0 or greater than 12). Throw a string exception to be caught by a catch block accepting a string. Output the message thrown by the exception indicating the month is an invalid month. Year less than 1900 or greater than 2020. Throw an integer exception to be caught by a catch block accepting an integer. In the catch block, if the integer thrown is less than 1900, output the message “You are very old”. If the integer exception is greater than 2020, output the message “You have not been born yet!” If neither exception occurs, simply output (cout) a message with the user’s birth month and year, for example, “You were born in “ << month << “ and “ << year. Declare any variables needed.…arrow_forwardThis is what I have so far 2. Too few parameters #include <stdio.h> Void simple(); Int main() { Int n; simple(1,2,3,4,5,6,7,8,9,10,11,12); Return0; } Void simple(int n){ printf(“%d”,n); printf(%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d) } What gets printed? The values 1 and the garbage values get printed. Where do the values come from? The values are some random values generated for the storage part failures like this where we are not allocating values but using them. Why? 1 is printed as per the statement. Garbage values are generated because of not having any argument passed to them to locate a specific location. 3. Too small arrayarrow_forwardThis is a debugging question - The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. Code I was given - // Catch exceptions for array index out of bounds // or dividing by 0 import java.util.*; public class DebugTwelve3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String inStr; int num, result; int[] array = {12, 4, 6, 8}; System.out.println("Enter a number ") inStr = input; num = Integer.parse(inStr); try { for(int x = 0; x < array.length; ++x) { result = array[x] / num; System.out.println("Result of division is " + result); result = array[num]; System.out.println("Result accessing array is " + result); } } catch(ArithmeticException…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
.2: Function Parameters and Arguments - p5.js Tutorial; Author: The Coding Train;https://www.youtube.com/watch?v=zkc417YapfE;License: Standard Youtube License