Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
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.
Part 2 - OddOrEven ClassIn bluej. Write a program that prompts the user to enter an integer. The program should display “The input is odd" to the screen if the input is odd and displays “The input is even" to the screen if the input is even. Hint: Consider using the mod (%) operator.
Consider 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. 876
Chapter 16 Solutions
Problem Solving with C++ (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
- For Java Write code for an if-else block that checks separates integers into the following categories: An integer less than 0, An integer greater than 0 and less than or equal to 10, An integer greater than 10 that is even, and an integer greater than 10 that is odd. Have each category print out a different statement. What are some ways you can make your code more efficient when using if-else blocks?arrow_forwardSolve using JAVAarrow_forwardJava 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_forward
- this 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_forwardjava code quation using ExceptionProgramme Leader of ITMB wants to check whether the student number and GSM numberentered by the student for the programming contest is valid or not.Write a Java program to read the Student ID and GSM Number of a student. Use a methodcalled ValidityDetails () for checking the validity of details entered.If the Student ID ends with the characters ST and contains more than 6 letters or if the MobileNumber does not contain exactly 8 digits, throw a user defined exceptionInvalidDetailsException.If the details entered are valid, display the message ‘”correct details are entered!!!” otherwisedisplay “Entered invalid details!!!!”arrow_forwardmain.py Load default template... 1 # Define your method here 2 3 if main ': name 4 # Type your code here.arrow_forward
- The program parses the input into integer and catch possible problems with the value provided. Update this code to use TryParse() method. Run it. Remove try-catch block completely. Run it using System; using static System.Console; namespace CheckingForOverflow { class Program { static void Main(string[] args) { try { checked { int x = int.MaxValue - 1; WriteLine($"Initial value: {x}"); x++; WriteLine($"After incrementing: {x}"); x++; WriteLine($"After incrementing: {x}"); x++; WriteLine($"After incrementing: {x}"); } } catch (OverflowException) { WriteLine("The code overflowed but I caught the exception."); } unchecked { int y = int.MaxValue + 1; WriteLine($"Initial value: {y}"); y--; WriteLine($"After decrementing: {y}"); y--; WriteLine($"After decrementing:…arrow_forwardUse the following code to answer the question public class SpeedTrap {private int speedLimit;/*** Creates a new SpeedTrap with the given* speed limit* * @precondition speedLimit >= 1* @postcondition getSpeedLimit()==speedLimit*/public SpeedTrap(int speedLimit) {if (speedLimit < 1) {throw new IllegalArgumentException("invalid speed limit");}this.speedLimit = speedLimit;}/*** Gets the speed limit* @return the speed limit*/public int getSpeedLimit() {return this.speedLimit;}/*** Checks to see if the given miles-per-hour* is considered speeding.** @precondition mph >= 0* @postcondition none** @returns true if speeding; false otherwise*/public boolean isSpeeding(int mph) {if (mph < 0) {throw new IllegalArgumentException("invalid mph");}return (mph > this.speedLimit);}}Write a JUnit test named testWhenSpeeding that uses a value for mph that is valid and is considered speeding.arrow_forwardUse the following code to answer the question public class SpeedTrap {private int speedLimit;/*** Creates a new SpeedTrap with the given* speed limit* * @precondition speedLimit >= 1* @postcondition getSpeedLimit()==speedLimit*/public SpeedTrap(int speedLimit) {if (speedLimit < 1) {throw new IllegalArgumentException("invalid speed limit");}this.speedLimit = speedLimit;}/*** Gets the speed limit* @return the speed limit*/public int getSpeedLimit() {return this.speedLimit;}/*** Checks to see if the given miles-per-hour* is considered speeding.** @precondition mph >= 0* @postcondition none** @returns true if speeding; false otherwise*/public boolean isSpeeding(int mph) {if (mph < 0) {throw new IllegalArgumentException("invalid mph");}return (mph > this.speedLimit);}}Write a JUnit test for the SpeedTrap constructor named testWhenOneUnderSpeedLimitBoundaryarrow_forward
- Assume that the SampleQueue class, with the given code below, is used in the TestSampleQueue that is also shown below. Show the output of the TestSampleQueue. Do the exercise without using a computer (i.e. Make amanual trace of the program):// TestSampleQueue classimport java.io.*;import java.lang.*;public class TestSampleQueue {private SampleQueue<Integer> queue = new SampleQueue<Integer>();public void run() throws Exception {queue.enqueue(new Integer(10));System.out.println(queue.toString());queue.enqueue(new Integer(5));System.out.println(queue.toString());queue.dequeue();System.out.println(queue.toString());queue.enqueue(new Integer(15));System.out.println(queue.toString());queue.enqueue(new Integer(7));System.out.println(queue.toString());queue.dequeue();System.out.println(queue.toString());} // end of run methodpublic static void main(String[] args) {TestSampleQueue program;try {program = new TestSampleQueue();program.run();} catch ( Exception e )…arrow_forwardWhat are the errors in the following code?arrow_forwardusing System;using System.Text.RegularExpressions;class chapter8 {static void Main() {string words = "08/14/57 46 02/25/59 45 06/05/85 18" +"03/12/88 16 09/09/90 13";string regExp1 = "(\\s\\d{2}\\s)";MatchCollection matchSet = Regex.Matches(words,regExp1);foreach (Match aMatch in matchSet)Console.WriteLine(aMatch.Groups[0].Captures[0]);}} Now let’s modify this program to search for dates instead of ages, and usea grouping construct to organize the dates. And write the c# code: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