Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 16.1, Problem 1STE
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
Question 3
}
public class WeekDay {
static void daysFromSunday(int day) {
String dayName = "";
try {
}
}
if (day > 7) {
throw new Exception("Invalid Day! Too high!");
}
else if(day < 1){
throw new Exception("Invalid Day! Too low!");
}
switch(day){
case 1: dayName="Sun";
break;
case 2: dayName="Mon";
break;
case 3: dayName="Tue";
break;
case 4: dayName="Wed";
break;
}
▬▬
}
case 5: dayName="Thu";
Dreak;
case 5: dayName="Thu";
break;
case 6: dayName="Fri";
break;
default: dayName="Sat";
System.out.println(dayName);
}
catch (Exception excpt) {
System.out.println("Oops");
System.out.println(excpt.getMessage());
}finally{
System.out.println("Done!");
public static void main(String[] args) {
daysFromSunday(10);
daysFromSunday(3);
daysFromSunday(0);
OOops
Invalid Day! Too high!
Tue
Oops
Invalid Day! Too low!
OOops
Invalid Day! Too high!
Done!
Tue
Done!
Oops
Invalid Day! Too low!
Done!
O Oops
Invalid Day! Too high!
Done!
Oops
Invalid Day! Too high!
Done!
Oops
Invalid Day! Too low!
Done!
O Oops…
Please 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);…
There is no maximum number of arguments that may be used inside a catch block since this kind of block does not have a parameter restriction.
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
- PYTHON: A pedometer treats walking 2,000 steps as walking 1 mile. Write a steps_to_miles() function that takes the number of steps as a parameter and returns the miles walked. The steps_to_miles() function throws a ValueError object with the message "Exception: Negative step count entered." when the number of steps is negative. Complete the main() program that reads the number of steps from a user, calls the steps_to_miles() function, and outputs the returned value from the steps_to_miles() function. Use a try-except block to catch any ValueError object thrown by the steps_to_miles() function and output the exception message. Output each floating-point value with two digits after the decimal point, which can be achieved as follows:print('{:.2f}'.format(your_value)) Ex: If the input of the program is: 5345 the output of the program is: 2.67 Ex: If the input of the program is: -3850 the output of the program is: Exception: Negative step count entered.arrow_forwardReview the code below. Fig 1 The output of the code is: Fig 2arrow_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
- PARTICIPATION ACTIVITY Arrange the following lines to make a program that determines when the number of people in a restaurant equals or exceeds 10 occupants. The program continually gets the number of people entering or leaving the restaurant. Ex: 2 means two people entered, and -3 means three people left. After each input, the program outputs the number of people in the restaurant. Once the number of people in the restaurant equals or exceeds 10, the program exits. If an Input MismatchException exception occurs, the program should get and discard a single string from input. Ex: The input "2 abc 8" should result in 10 occupants. Not all lines are used in the solution. How to use this tool ✓ Unused totalNumPeople += scnr.nextInt (); while (totalNumPeople < maxNumPeople) { } } 11.1.5: Handling input exceptions: restaurant max occupancy tracker. catch { try { catch (InputMismatchException e) { scnr .nextLine(); scnr.next(); System.out.println("Error"); } System.out.println("Occupancy:…arrow_forwardDIRECTION: Using Java as program language Create a simple program for a quiz bee with a class named QuizBee. The program shall:o Contain an array of 15 multiple choice questions with three (3) choiceseach and;o Require the user to choose among A, B or C.o Note: Cases are ignored. Lowercase letters are acceptable (a,b, or c) Create a try-catch structure to handle three (3) exceptions. These are when theuser inputs the following:o An invalid letter (not A, B or C)o A number or any special character.o Blank (no answer). Prompt the user to answer again if any of the three (3) exceptions is thrown. Display the score, number of incorrect answers and corrects answers.arrow_forwardThere is no limit to the number of arguments that can be used in a catch block.arrow_forward
- Which of the following members take values in test object: * Class Test (int xyzı; Static int xyz;; Static int sum(){} Void av(){} }test; а-хуz1 b-хуz2 C- sum d- av e- both a&barrow_forwardThere is no limit to the number of arguments that may be put into a catch block.arrow_forwardPlease debug this code. // Handles a Format Exception if user does not enter a number using System; using static System.Console; using System.Globalization; class DebugEleven01 { static void Main() { double salary; string salVal; bool isValidSalary; while(!isValidSalary) { try Write("Enter an employee's salary "); salVal = ReadLine(); salary = Convert.ToDouble(salVal); isValidSalary = true; catch(Formatexception) { WriteLine("You must enter a number for the salary."); } } WriteLine("The salary {0} is valid", salary.ToString("C2", CultureInfo.GetCultureInfo("en-US"))); } }arrow_forward
- I need the answer within 15 minutesarrow_forward1-Write a JAVA program that reads an array from input file and invokes twodifferent methods Sort and Max ,that sorts the elements of the array and findsthe Max element and writes out the resulted array in to output file . Usetwo interfaces for methods and throw Exception Handling for Out Of Boundindex for the arrayarrow_forwardThe following class maintains an account balance and returns a special error code. public class Account { private double balance; // returns new balance or -1 if error public double deposit(double amount) { if (amount > 0) balance += amount; else return -1; // Code indicating error return balance; } } Rewrite the class so that it throws appropriate exception instead of returning -1 as an error code. Write test code that attempts to depositarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning