Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 16.1, Problem 7STE
Program Plan Intro
Exception:
An exception is a problem that creates during the execution of a program; it offers a method to transfer control from one part to another part of a program.
An exception handling is created by using the following three keywords such as try, catch and throw.
- The “try” block have the program for the basic
algorithm that says the computer what to do when all goes well. - The “throw” keyword throws an error statement to the “catch” block.
- The “catch” block will catch the exception or handling the exception.
Generally, the compiler executes “try” block. In the “try” block, if the statements cause an exception, it throws an error statement to the “catch” block using the keyword “throw”. The “catch” block then handles the error based upon the type of exception.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Suppose you are asked to test the code below. The code:
public String allocateRoom(int numOfEmps) throws IllegalArgumentException {
if (numOfEmps == 0)
throw new IllegalArgumentException();
if (numOfEmps < 5)
return "Small";
if (numOfEmps < 10)
return "Standard"; return "Large";
}
Throws exception if there are no employees
Returns small up to 5 employees
Returns standard for 5 to 9 employees
Returns large for 10 or more employees
List the minimum amount of test inputs that achieve 100% branch coverage. You need to specify the branch(es) each input covers.
List the minimum amount of test inputs that achieve 100% path coverage. You need to specify the path each input covers.
Explain if executing the test suites above will reveal any defects.
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;
You are to develop a set ofunit testcasesfor the following code block.Note that the code below 5 may contain some faults. The test cases you write should catch these faults and any others that might arise as the code evolves.
Write at-least 5 tests, make sure that your tests cover and test evert aspect of the code.
Chapter 16 Solutions
Problem Solving with C++ (10th 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
- Java Exception method withdraw throws an exception if amount is greater than balance. For example: Test Result Account account = new Account("Acct-001","Juan dela Cruz", 5000.0); account.withdraw(5500.0); System.out.println("Balance: "+account.getBalance()); Insufficient: Insufficient funds. Balance: 5000.0 Account account = new Account("Acct-001","Juan dela Cruz", 5000.0); account.withdraw(500.0); System.out.println("Balance: "+account.getBalance()); Balance: 4500.0arrow_forwardUsing C++ Write a void function GetYear that prompts for and inputs the year the operator was born (type int) from standard input. The function returns the user’s birth year through the parameter list (use pass by reference) unless the user enters an invalid year, in which case a BadYear exception is thrown. To test for a bad year, think about the range of acceptable years. It must be 4 digits (i.e. 1982) and it cannot be greater than the current year (the user obviously cannot be born in a calander year that has yet to happen!arrow_forwardplease solve this important, thanks.arrow_forward
- What is the value stored at x, given the statements? int x; x = 6 / static_cast<int> (4.5 + 6.4);arrow_forwardCreate a Junit Parameterized Test for Testing Factorials from n= 0 to 30? Take snapshots of .java, Test.java files and output of test cases. Also upload codearrow_forwardConsider the following code: def test(*x): for k in x: print(type(k)) what will be the output if test function is culled with 876? Oa. O b. O c. O d. 876arrow_forward
- Python please: Write a square_root() function that takes a number and returns the square root. The square_root() function throws a ValueError object with the message "Exception: Negative input for square root entered." when the number is negative. Complete the main() program that reads a number from a user, calls the square_root() function, and outputs the returned value from the square_root() function. Use a try-except block to catch any ValueError object thrown by the square_root() function and output the exception message. If the user enters something other than an integer, the program throws a ValueError object with the message "invalid literal for int() with base 10: " and 'user_input'. Output each floating-point value with two digits after the decimal point, which can be achieved as follows: print('{:.2f}'.format(your_value)) Example: If the input is 100 the output is 10.00 Example: If the input is -55 the output is Exception: Negative input for square root entered. Example: If…arrow_forward6. Which of the below code contains correct overloading code? Why would the other won't work? Code 1 double myMultiply. (int x){ return 10 x; Code 2 int awMultielx (int x){ return 10 x; } double yMultirly. (double x){ double yMultiely (int x){ return 20 x; return 2e * x; 7. What would be the output for the following program and why ( explain in short): Bublis static void nain(String acsal))( String."; String si - "Testing Java Program String s2 - sukstriui13); 8. Write the code in Java for the following class from the UML diagram below: Vehicle -passengers: int -maxSpeed: double -color: String +Vehiclelint,double.String) +setColor(): void +getColor(): String +findCaracityl): double The findCapacityl) method return the capacity of the vehicle by the following formula: Capacity = passengers * 75 L0.20 * maxSpeed)arrow_forwardThe following class maintains an account balance and returns aspecial error code.public class Account{private double balance;// returns new balance or -1 if errorpublic double deposit(double amount){if (amount > 0)balance += amount;elsereturn -1; // Code indicating errorreturn balance;}}Rewrite the class so that it throws appropriate exception instead ofreturning -1 as an error code. Write test code that attempts to depositinvalid amounts and catches the exceptions that are thrownarrow_forward
- Java Language How can I create a test in JUnit for this setter? public void setAge(int age) throws IllegalArgumentException{ if ( age < 0 || age > 80 ) throw new IllegalArgumentException("Invalid age entered"); this.age = age; }arrow_forwardPlease help, write the code for the test cases: public class JunitTest_RideRequestTest { // Test parameterized constructor with invalid input @Test public void test_2_0() { try { RideRequest request = new RideRequest(new String()); fail("You should throw an exception If the input is null or empty."); } catch (IllegalArgumentException e) { // exception excepted do nothing } } @Test public void test_2_2() { RideRequest request = null; try { RideRequest request2 = new RideRequest(request); fail("You should throw an exception If the input is null."); } catch (IllegalArgumentException e) { // exception excepted do nothing } } @Test public void test_2_3() { String s = "John , Downtown , 50.0 , Y , extra info"; try { RideRequest request2 = new RideRequest(s);…arrow_forwardthis practice assignment wants me to : Write an application that asks a user to enter an integer. Display a statement that indicates whether the integer is even or odd. it is saying that i am missing a return statement. here is my code: mport java.util.Scanner; class EvenOdd { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number; System.out.println("Enter the integer >>>"); number = input.nextInt(); } public static boolean isEven(int number) { if (number % 2 == 0) System.out.println("The number is even."); else System.out.println("The number is odd."); } }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