MYPROGRAMMINGLAB WITH PEARSON ETEXT
8th Edition
ISBN: 9780134225340
Author: Deitel
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 22, Problem 22.31E
Program Plan Intro
- To define a function first which throws an exception.
- To define four more functions and make them call the previous function i.e. create a nested function. For example, the function second calls the function first and so on.
- Use a catch handler to catch the exception and display the result.
Summary Introduction- The program throws an exception in the deeply nested function and catches in the main.
Program Description- The purpose of the program to throw an exception in the deeply nested function and catches in the main.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
(Creating Custom Exception Class) Write a Password class that stores a password string. Passwords must contain at least seven alpha-numeric characters, with at least one letter and one digit. Also write an InvalidPasswordException class that extends Exception. The Password constructor should throw an InvalidPasswordException, which should report an appropriate message based on a given string (e.g. if the string contains no digits, the exception should report this fact). Write a GUI program to prompt the user to enter a password and report an InvalidPasswordException if an invalid password is entered. Please teach and help me how to deisgn the program based on the two files given .Some sample screenshots are shown below:
InvalidPasswordException.java
public class InvalidPasswordException extends Exception{ public InvalidPasswordException (String errMsg){ super(errMsg); } }
Password.java
public class Password {private String passwordstr;
public…
10) In C++, try-catch blocks can be nested. Also, an exception can be thrown again using “throw; ” true or false
8) Exceptions are run-time anomalies or abnormal conditions that a program encounters during its execution
true or false
Instructions:
Exception handing
Add exception handling to this game (try, catch, throw). You are expected to use the standard exception classes to try functions, throw objects, and catch those objects when problems occur.
At a minimum, you should have your code throw an invalid_argument object when data from the user is wrong.
File GamePurse.h:
class GamePurse
{
// data
int purseAmount;
public:
// public functions
GamePurse(int);
void Win(int);
void Loose(int);
int GetAmount();
};
File GamePurse.cpp:
#include "GamePurse.h"
// constructor initilaizes the purseAmount variable
GamePurse::GamePurse(int amount){
purseAmount = amount;
}
// function definations
// add a winning amount to the purseAmount
void GamePurse:: Win(int amount){
purseAmount+= amount;
}
// deduct an amount from the purseAmount.
void GamePurse:: Loose(int amount){
purseAmount-= amount;
}
// return the value of purseAmount.
int GamePurse::GetAmount(){
return purseAmount;
}
File…
Chapter 22 Solutions
MYPROGRAMMINGLAB WITH PEARSON ETEXT
Ch. 22 - Prob. 22.15ECh. 22 - (Catch Parameter) Under what circumstances would...Ch. 22 - (throw Statement) A program contains the statement...Ch. 22 - (Exception Handling vs. Other Schemes) Compare and...Ch. 22 - Prob. 22.19ECh. 22 - Prob. 22.20ECh. 22 - Prob. 22.21ECh. 22 - (Catching Derived-Class Exceptions) Use...Ch. 22 - Prob. 22.23ECh. 22 - Prob. 22.24E
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 Code: Write a function that takes a single number as an argument: 1) This function should then check whether a number is an even number (2,4,6,8) and raise an exception if otherwise 2) Call this function with an uneven number first without catching the exception and then with catching the exception and printing a warning to the user afterwards Bonus: Do the same as the above but instead implement your solution for prime numbers and call the function with a non-prime numberarrow_forward[Access Control Policy]: A computer system usually implements the exception handling mechanism. Like the interrupt handling mechanism, the original program flow will be stopped until the exception is handled. For example, when a program is calculating c=a/b, and for some reason b was set to 0, the program will encounter the "Division by Zero" exception. The control will be given to the exception handler, and the recovery action could be "report only", "return to the next instruction", "purge the program", etc., based on the severity of the problem. From the abov information, we can conclude: A computer system can integrate both Discretionary Access Control (DAC) and Mandatory Access Control (MAC) security policies An administrator can inhibit the exception handling The exception handling mechanism is like the Mandatory Access Control security policy, no matter which user caused the exception (even the administrator), the program should be stopped, and the control should be given to the…arrow_forward(Throwing Exceptions from a catch) Suppose a program throws an exception and the appropriate exception handler begins executing. Now suppose that the exception handler itself throwsthe same exception. Does this create infinite recursion? Write a program to check your observation.arrow_forward
- What is the purpose of exception specifications? How do you declare a throw list? Can you declare multiple exceptions in a function declaration?arrow_forwardExceptions are caught and handled by usingarrow_forwardDirection:Analyze the given problem and create a java program using exception. Problem:Create a program that will accept any real number then,converts the number to fractional number,and multiply the value by 9.arrow_forward
- Subject: Object Oriented PrgrammingLanguage: Java ProgramTopic: Exception Define a new exception, called ExceptionLineTooLong, that prints out the error message "The strings is too long". Write a program that reads phrase and throws an exception of type ExceptionLineTooLong in the case where a string is longer than 80 characters. EXAMPLE: Input:The quick brown fox jumped over the lazy dogs. Output:The quick brown fox jumped over the laze dogs. ANOTHER EXAMPLE: Input:The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick…arrow_forwardDefine the process of Generating the address of an exception handler ?arrow_forward"Determine which exception is needed for each case and put them in the corresponding catch statement with the appropriate variable. Do NOT use the general exception handler Exception for any one. Possible Exceptions: IOException, ArithmeticException, StringIndexOutOfBoundsException, NullPointerException, ArrayIndexOutOfBoundsException" public String catchErrors(int choice) {switch (choice) {case 1:try {String word;word = null;word.charAt(0);} catch () { //TODOreturn e.toString();}break;case 2:try {int[] array = new int[5];array[5] = 11;} catch () { //TODOreturn e.toString();}break;case 3:try {int ans = 7 / 0;} catch () { //TODOreturn e.toString();}break;}return null;}arrow_forward
- Q4: Write a program that performs exception handling. A class should have two exception handling classes. This program throws an exception of type both classes (user defined data types) and they are catched in a catch block. This class also throws an exception of built-in data types (of your choice). Also in your program include a catch block for all data types. Write a simple C++ code as i am a begineer.arrow_forwardDesign a Customized Exception Java program * Consider a case and write a short description about your case. *Draw a flowchart of your designarrow_forwardWhat happens if an exception goes uncaught? which one: The program aborts execution. f the exception is thrown within a function, control passes back to main. The program exits the try block, but continues with execution. None of the above.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