Starting Out With C++: Early Objects (10th Edition)
10th Edition
ISBN: 9780135235003
Author: Tony Gaddis, Judy Walters, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
Chapter 16.1, Problem 16.4CP
Explanation of Solution
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
Process of the “try/catch” blocks:
- The “try” block contains a block of statements which may cause an exception to be thrown.
- Once an exception is caught, it has to be thrown to the “catch” block using “throw” statement. After executing the “catch” block, the program resumes at the statement, which follows the “try/catch” blocks.
Example:
For example, consider the below code containing an exception handler for dividing two integers:
#include <iostream>
#include <string>
using namespace std;
//function definition of "divide"
double divide(int a, int b)
{
//The value of "b" is 0
if (b == 0)
{
//declare and define the string
string exception="ERROR: denominator should not be 0...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
If an exception is raised and the program does not handle it with a try/exceptstatement, what happens?
If an exception is thrown outside a try block, what are the probable outcomes?
Is there any way to skip Finally block of exception even if some exception occurs in the exception block?
Chapter 16 Solutions
Starting Out With C++: Early Objects (10th 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.2 - Prob. 16.6CPCh. 16.2 - The function int minPosition(int arr[ ], int size)...Ch. 16.2 - What must you be sure of when passing a class...Ch. 16.2 - Prob. 16.9CPCh. 16.4 - Prob. 16.10CP
Ch. 16.4 - In the following Rectangle class declaration, the...Ch. 16 - The line containing a throw statement is known as...Ch. 16 - Prob. 2RQECh. 16 - Prob. 3RQECh. 16 - Prob. 4RQECh. 16 - The beginning of a template is marked by a(n)...Ch. 16 - Prob. 6RQECh. 16 - A(n)______ container organizes data in a...Ch. 16 - Prob. 8RQECh. 16 - Prob. 9RQECh. 16 - Prob. 10RQECh. 16 - Write a function template that takes a generic...Ch. 16 - Write a function template that is capable of...Ch. 16 - Describe what will happen if you call the function...Ch. 16 - Prob. 14RQECh. 16 - Each of the following declarations or code...Ch. 16 - Prob. 16RQECh. 16 - String Bound Exceptions Write a class BCheckString...Ch. 16 - Prob. 2PCCh. 16 - Prob. 3PCCh. 16 - Sequence Accumulation Write n function T...Ch. 16 - Rotate Left The two sets of output below show the...Ch. 16 - Template Reversal Write a template function that...Ch. 16 - SimpleVector Modification Modify the SimpleVector...Ch. 16 - Prob. 8PCCh. 16 - Sortabl eVector Class Template Write a class...Ch. 16 - Prob. 10PCCh. 16 - Word Transformers Modification Modify Program...Ch. 16 - Prob. 12PC
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
- How can the standard error message be shown when an exception is thrown?arrow_forwardWhat happens when an unhandled exception occurs? The finally clause executes, and then the exception is re-raised. O The finally clause executes, and then runs the try block again. O The finally clause executes, and the code finishes. O The finally clause executes, and the code returns -1.arrow_forwardIf an exception is thrown, how do you get out of break mode?arrow_forward
- in pytrhon programming language, What happens when an unhandled exception occurs? The finally clause executes, and the code finishes. The finally clause executes, and the code returns -1. The finally clause executes, and then the exception is re-raised. The finally clause executes, and then runs the try block again.arrow_forwardWrite a program containing a possible exception. Use a try block to throw it and a catch block tohandle it promptly.arrow_forwardHow do you get out of break mode when an exception is thrown?arrow_forward
- could you include a try-catch statement that calls the GetYear function defined above. If a BadYear exception is thrown, print an error message and rethrow the exception to a caller; otherwise, execution should just continue as normal.arrow_forwardWhat happens if an exception is thrown, but not caught?arrow_forwardWrite a program that throw s an exception from a deeply nested function and still has the catch handler following the try block enclosing the initial call in main catch the exception.arrow_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,C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
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