STARTING OUT C++,+MATLAB+MYPROGRAMLAB>C
STARTING OUT C++,+MATLAB+MYPROGRAMLAB>C
19th Edition
ISBN: 9781323948637
Author: GADDIS
Publisher: PEARSON C
bartleby

Concept explainers

Question
Book Icon
Chapter 16, Problem 22RQE
Program Plan Intro

Exception:

In C++, the exception is an object used to signal the unexpected error occurred during a program execution.

  • 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:

STARTING OUT C++,+MATLAB+MYPROGRAMLAB>C, Chapter 16, Problem 22RQE , additional homework tip  1try

{

//code which causes an exception;try block

}

STARTING OUT C++,+MATLAB+MYPROGRAMLAB>C, Chapter 16, Problem 22RQE , additional homework tip  2Catch(Exception_argument)

{

//code which handles the exception;catch block

}

//Repeat catch blocks as many as needed

Blurred answer
Students have asked these similar questions
Exception 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…
public Movie remove(Movie movie) {// Step 1: Throw an exception if the thing you want to remove is null// Step 2: Search through the array and try to find movie.Movie removed = null;/* *for (int i = 0;...) { *if(movies[i].equals(movie)) {* ... *} *} */// Step 3: Throw an exception if you failed to find the movie,// else return the movieif (removed == null) {// Throw exception if movie is still null}return movie;}
(Stack Unwinding) Write a program that throws an exception from a deeply nested function and still has the catch handler following the try block enclosing the initial call in main catchthe exception.

Chapter 16 Solutions

STARTING OUT C++,+MATLAB+MYPROGRAMLAB>C

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 - The line containing a throw statement is known as...Ch. 16 - Prob. 11RQECh. 16 - Prob. 12RQECh. 16 - Prob. 13RQECh. 16 - The beginning of a template is marked by a(n)...Ch. 16 - Prob. 15RQECh. 16 - Prob. 16RQECh. 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. 21RQECh. 16 - Prob. 22RQECh. 16 - Prob. 23RQECh. 16 - Prob. 24RQECh. 16 - T F All type parameters defined in a function...Ch. 16 - Prob. 26RQECh. 16 - T F A class object passed to a function template...Ch. 16 - Prob. 28RQECh. 16 - Prob. 29RQECh. 16 - Prob. 30RQECh. 16 - Prob. 31RQECh. 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. 34RQECh. 16 - Prob. 35RQECh. 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. 39RQECh. 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. 8PCCh. 16 - Prob. 9PCCh. 16 - SortableVector Class Template Write a class...Ch. 16 - Inheritance Modification Assuming you have...Ch. 16 - Prob. 12PCCh. 16 - Prob. 13PC
Knowledge Booster
Background pattern image
Computer Science
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
Recommended textbooks for you
  • Text book image
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT