Starting Out with C++ from Control Structures to Objects (8th Edition)
8th Edition
ISBN: 9780133769395
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 16, Problem 32RQE
Program Plan Intro
Exception:
In C++, the exception is an object used to signal the unexpected error occurred during a
- An exception has been thrown when an error has been occurred in a program.
Exception Handler:
The “throw point” is a statement which uses the keyword “throw” to throw an exception which occurred while executing the program.
- After the execution of “throw” statement, the program control will move to another part in the same program to handle the exception is called as “exception handler”.
- In order to handle an exception, construct a “try/catch” block in a program.
The “try” block:
- The “try” block is starts with the keyword “try” and it contains the code which will have the possibility to generate an exception.
- The “try” block must be followed by one or more “catch” blocks which are denoted as exception handlers.
The “catch” block:
- The “catch” block is starts with the keyword “catch” and it is followed by an “exception parameter”.
- The Catch block contains the code to be executed when the error occurred in Try block.
The general syntax for “try/catch” as follows:
try
{
//code which causes an exception;try block
}
Catch(Exception_argument)
{
//code which handles the exception;catch block
}
//Repeat catch blocks as many as needed
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
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:…
Java
Pls help ASAP
Chapter 16 Solutions
Starting Out with C++ from Control Structures to Objects (8th Edition)
Ch. 16.1 - Prob. 16.1CPCh. 16.1 - Prob. 16.2CPCh. 16.1 - Prob. 16.3CPCh. 16.1 - Prob. 16.4CPCh. 16.1 - Prob. 16.5CPCh. 16.3 - Prob. 16.6CPCh. 16.3 - The following function accepts an i nt argument...Ch. 16.3 - Prob. 16.8CPCh. 16.3 - Prob. 16.9CPCh. 16.4 - Prob. 16.10CP
Ch. 16.4 - Prob. 16.11CPCh. 16 - Prob. 1RQECh. 16 - Prob. 2RQECh. 16 - Prob. 3RQECh. 16 - Prob. 4RQECh. 16 - What is unwinding the stack?Ch. 16 - What happens if an exception is thrown by a classs...Ch. 16 - How do you prevent a program from halting when the...Ch. 16 - Why is it more convenient to write a function...Ch. 16 - Why must you be careful when writing a function...Ch. 16 - Prob. 10RQECh. 16 - Prob. 11RQECh. 16 - Prob. 12RQECh. 16 - The line containing a throw statement is known as...Ch. 16 - Prob. 14RQECh. 16 - Prob. 15RQECh. 16 - Prob. 16RQECh. 16 - The beginning of a template is marked by a(n)...Ch. 16 - Prob. 18RQECh. 16 - Prob. 19RQECh. 16 - Prob. 20RQECh. 16 - Prob. 21RQECh. 16 - _____________ are pointer-like objects used to...Ch. 16 - Prob. 23RQECh. 16 - Write a function that searches a numeric array for...Ch. 16 - Write a function that dynamically allocates a...Ch. 16 - Make the function you wrote in Question 17 a...Ch. 16 - Write a template for a function that displays the...Ch. 16 - Prob. 28RQECh. 16 - Prob. 29RQECh. 16 - Prob. 30RQECh. 16 - Prob. 31RQECh. 16 - Prob. 32RQECh. 16 - Prob. 33RQECh. 16 - Prob. 34RQECh. 16 - T F All type parameters defined in a function...Ch. 16 - Prob. 36RQECh. 16 - T F A class object passed to a function template...Ch. 16 - Prob. 38RQECh. 16 - Prob. 39RQECh. 16 - Prob. 40RQECh. 16 - Prob. 41RQECh. 16 - T F A class template may not be derived from...Ch. 16 - T F A class template may not be used as a base...Ch. 16 - Prob. 44RQECh. 16 - Prob. 45RQECh. 16 - Prob. 46RQECh. 16 - Prob. 47RQECh. 16 - try { quotient = divide(num1, num2); } cout The...Ch. 16 - template class T T square(T number) { return T T;...Ch. 16 - template class T int square(int number) { return...Ch. 16 - Prob. 51RQECh. 16 - Assume the following definition appears in a...Ch. 16 - Assume the following statement appears in a...Ch. 16 - Prob. 1PCCh. 16 - Prob. 2PCCh. 16 - Prob. 3PCCh. 16 - Prob. 4PCCh. 16 - Prob. 5PCCh. 16 - IntArray Class Exception Chapter 14 presented an...Ch. 16 - TestScores Class Write a class named TestScores....Ch. 16 - Prob. 9PCCh. 16 - SortableVector Class Template Write a class...Ch. 16 - Inheritance Modification Assuming you have...Ch. 16 - Prob. 12PCCh. 16 - Prob. 13PCCh. 16 - 14. Test Scores vector
Modify Programming...Ch. 16 - Prob. 15PCCh. 16 - Prob. 16PCCh. 16 - Prob. 17PC
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
- Any number of arguments may be used in a catch block.arrow_forwardPurpose To become familiar with throwing and catching exceptions. Directions: The Fibonacci series is a famous sequence of numbers that occurs often in nature, architecture, painting, etc. (you can more read about it on Wikipedia if you are interested). The sequence is defined as follows: • The first Fibonacci number is 1 • The second Fibonacci number is also 1 • The nth Fibonacci number is the sum of the previous two numbers. The first few numbers in the sequence are therefore: 1, 1, 2, 3, 5, 8, 13, 21, 34, … Your task is to write a method called fibonacci that accepts an integer argument called n and returns the nth Fibonacci number. Because this method only makes sense for positive values of n, your method should throw an exception (which you will need to create) of type InvalidArgumentException. Your method should have the following signature: public static int fibonacci(int n) throws InvalidArgumentException Hint: One way to implement the fibonacci method is to have two variables…arrow_forwardCode to calling a block multiple times Write method can invoke a block as many times as it wants.arrow_forward
- I need fix this code Please help me def add(a,b): return a+b def subtract(a,b): return a-b def multiply (a,b): return a*b def divide(a,b): try: return a/b except Exception as e: print(e) def power(a,b): return a**b def remainder(a,b): return a%b def select_op(choice): if choice == '#': return -1 elif choice == '$': return 0 elif choice in ('+', '-', '*', '/', '^', '%'): while True: num1s = input("Enter first number: ") print(num1s) if num1s.endswith('$'): return 0 if num1s.endswith('#'): return -1 try: num1 = float(num1s) except: print("Not a valid number, please enter again") while True: num2s = input("Enter second number: ") print(num2s) if num2s.endswith('$'): return 0 if num2s.endswith('#'): return -1 try:…arrow_forwardC++ PROGRAM EXCEPTION HANDLING Write a program with a function that accepts a string from user. The function prints “Valid String” only when the string contains at least one capital letter, one vowel, one integer and one ‘@’ character. An example of a valid string is @Developer123. If the user enters any operators like +,*,= then your program must throw a character exception. If the string contains any brackets like {[(}]), then your program must throw an integer exception. Write appropriate catch blocks of these exceptions.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_forward
- Please 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_forwardException handling to detect input string vs. int C++ The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a string rather than an int. At FIXME in the code, add a try/catch statement to catch ios_base::failure, and output 0 for the age. Ex: If the input is: Lee 18 Lua 21 Mary Beth 19 Stu 33 -1 then the output is: Lee 19 Lua 22 Mary 0 Stu 34 #include <string>#include <iostream> using namespace std; int main() { string inputName; int age; // Set exception mask for cin stream cin.exceptions(ios::failbit); cin >> inputName; while(inputName != "-1") { // FIXME: The following line will throw an ios_base::failure. // Insert a try/catch statement to catch the exception. // Clear cin's failbit to put cin in a useable state. cin >> age; cout << inputName…arrow_forwardCode on phyton: Write a program that accepts an amino acid sequence. The outshould should be able to display the peptide fragments wherein this was obtained using the enzyme called chymotrypsin. Note that: This enzyme should cleave the carboxyl side of Phenylalanine, Tyrosine, and Tryptophan. Also remember that there is an exception to the rule when it comes to Proline. Output should be like this: Enter Amino Acid Sequence: MQKESLWARYLEIFVGNFPYL The peptide fragment/s are: MQKESLW ARY LEIF VGNFPY Larrow_forward
- Rectangle's Length and Width Code in C language // WARNING: Do not add, remove, or change anything before the line 19 of this file.// Doing so will nullify your score for the activity. #include <stdio.h>#include "rectangle.h" int get_length(Rectangle *rect);int get_width(Rectangle *rect); int main() { int ur_x, ur_y, ll_x, ll_y; printf("UR's X: "); scanf("%d", &ur_x); printf("UR's Y: "); scanf("%d", &ur_y); printf("LL's X: "); scanf("%d", &ll_x); printf("LL's Y: "); scanf("%d", &ll_y); // TODO: Initialize the points here // Point ... // TODO: Initialize the rectangle here // Rectangle ... // TODO: Call the get_length here int len = ___; printf("\nLength: %d", len); // TODO: Call the get_width here int wid = ___; printf("\nWidth: %d", wid); return 0;} // TODO implement get_lengthint get_length(Rectangle *rect) { return 0;} // TODO implement get_widthint get_width(Rectangle *rect){ return 0;} refer to pics for instructionsarrow_forwardFleet class: Instance Variables An array that stores Aircraft that represents Delta Airlines entire fleet A variable that represents the count for the number of aircraft in the fleet Methods: o Constructor-One constructor that instantiates the array and sets the count to aero readFile()-This method accepts a string that represents the name of the file to be read. It will then read the file. Once the data is read, it should create an aircraft and then pass the vehicle to the addAircraft method. It should not allow any duplication of records. Be sure to handle all exceptions. o writeFile()-This method accepts a string that represents the name of the file to be written to and then writes the contents of the array to a file. This method should call a sort method to sort the amay before writing to it. sortArray()-This method returns a sorted array. The array is sorted by registration number.u addAircraft- This method accepts an aircraft and adds it to the fleet(the array only if it is not…arrow_forwardCUSTOM EXCEPTION 1 Donor Eligibility Write a C++ program to handle the exception. Read all the details and check for the eligibilty of the donors. Conditions: Age must be greater than 17. Minimum weight should be greater than 44 kg. Maximum amount of blood can be drawn is 350ml. if any of the conditions are not met then throw an exceptibn Refer the sample input/output below. Input and Output Format: Refer sample input and output for formatting specifications. All text in bold corresponds to input and the rest corresponds to output. [All text in bold corresponds to input and P Type here to searcharrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr