Java Format: Unbound (saleable)
8th Edition
ISBN: 9780134448398
Author: SAVITCH, Walter
Publisher: Prentice Hall
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 9.1, Problem 7STQ
Explanation of Solution
“throw” statement:
- • The keyword “throw” is used to throw an exception from the
program . It is possible to throw either checked or unchecked exception. - • When an exception is thrown using “throw” keyword, the “try” block ends and the statements inside that block will not get executed. The control directly goes to the subsequent “catch” block whose parameter matches the exception object.
Example program:
//Main Class
class Main
{
//Main method
public static void main(String[] args)
{
/*A variable is declared and a value is assigned*/
int value = 9;
//Try block
try
{
//Check if value is greater than 30
if(value < 10)
//Throw an exception
throw new Exception("Value is lesser");
//Print the message
System...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
In the case that an exception is thrown outside of a try block, what kinds of outcomes are open to consideration?
Is the SecurityException type a checked or unchecked exception in the java.lang package? Must we not throw NoSuchMethodException? Be sure to provide reasons for your answers.
Re-run the code and verify that you are now creating Person objects. (Why do you think we did not need an import statement for you Person class like we did with the Scanner?) Also the output below seems to suggest there is a bug or some unintended behaviour -can you spot what it is?
Chapter 9 Solutions
Java Format: Unbound (saleable)
Ch. 9.1 - Prob. 1STQCh. 9.1 - What output would the code in the previous...Ch. 9.1 - Prob. 3STQCh. 9.1 - Prob. 4STQCh. 9.1 - Prob. 5STQCh. 9.1 - Prob. 6STQCh. 9.1 - Prob. 7STQCh. 9.1 - Prob. 8STQCh. 9.1 - In the code given in Self-Test Question 1,...Ch. 9.1 - In the code given in Self-Test Question 1,...
Ch. 9.1 - Prob. 11STQCh. 9.1 - Prob. 12STQCh. 9.1 - Prob. 13STQCh. 9.1 - Prob. 14STQCh. 9.2 - Prob. 15STQCh. 9.2 - Prob. 16STQCh. 9.2 - Prob. 17STQCh. 9.2 - Prob. 18STQCh. 9.2 - Prob. 19STQCh. 9.2 - Prob. 20STQCh. 9.2 - Suppose that, in Self-Test Question 19, we change...Ch. 9.2 - Prob. 22STQCh. 9.2 - Prob. 23STQCh. 9.3 - Prob. 24STQCh. 9.3 - Prob. 25STQCh. 9.3 - Prob. 26STQCh. 9.3 - Prob. 27STQCh. 9.3 - Prob. 28STQCh. 9.3 - Repeat Self-Test Question 27, but change the value...Ch. 9.3 - Prob. 30STQCh. 9.3 - Prob. 31STQCh. 9.3 - Prob. 32STQCh. 9.3 - Consider the following program: a. What output...Ch. 9.3 - Write an accessor method called getPrecision that...Ch. 9.3 - Prob. 35STQCh. 9.4 - Prob. 36STQCh. 9.4 - Prob. 37STQCh. 9.4 - Prob. 38STQCh. 9 - Write a program that allows students to schedule...Ch. 9 - Prob. 2ECh. 9 - Prob. 3ECh. 9 - Prob. 4ECh. 9 - Prob. 5ECh. 9 - Write code that reads a string from the keyboard...Ch. 9 - Create a class Rational that represents a rational...Ch. 9 - Prob. 9ECh. 9 - Suppose that you are going to create an object...Ch. 9 - Revise the class RoomCounter described in the...Ch. 9 - Prob. 12ECh. 9 - Write a class LapTimer that can be used to time...Ch. 9 - Prob. 1PCh. 9 - Prob. 2PCh. 9 - Prob. 3PCh. 9 - Write a program that uses the class calculator in...Ch. 9 - Prob. 3PPCh. 9 - Prob. 7PPCh. 9 - Suppose that you are in change of customer service...Ch. 9 - Write an application that implements a trip-time...
Knowledge Booster
Similar questions
- What is the difference between using @Test(expected = TypeOfException) and using try/catch when testing if a method or constructor properly returns an exception? A. They do the same thing. B. @Test(expected = ...) is the proper way to unit test in java, and try/catch should only be used when unit testing in Python. C. @Test(expected = ...) will give a passing test as soon as one exception of the proper type is thrown and then stop the test, but using try/catch will allow you to test many examples in the same test and will only stop after running all examples or reaching a fail() or an assert statement that fails. D. We should never use try/catch when unit testing, otherwise we will catch the exception and then won't know if it is actually thrown.arrow_forwardThank you much. The code doesn't return the right answers for the test cases. Is there a way to fix?arrow_forwardCould you please explain the Write Through method?arrow_forward
- Describe in your own words what happens if you do not handle exceptions? What is the finally block?arrow_forwardLet's revisit chapter 5 example, but this time, no method can throw any exception out of the method and if an exception was detected then you will have to throw your own custom exception to let the user know what happened. public class Chapter11Demo{ public static void main(java.lang.String[] args) {method1();}private static void method1(){ method2();}private static void method2(){ method3();}private static void method3(){ method4();}private static void method4(){ method5();}private static void method5(){ method6();}private static void method6(){ java.io.File in=new java.io.File("somefile.txt"); java.io.File out=new java.io.File("somefile2.txt"); java.util.Scanner inFile=new java.util.Scanner(in); java.io.FileWriter outFile=new java.io.FileWriter(out); }}arrow_forwardLet us assume that there is a catch handler that perfectly fits the kind of exception object being thrown. What are the circumstances in which exception objects of that type could be treated differently than they normally would be?arrow_forward
- I am getting this error with the EXCEPTIONarrow_forwardWhat happened after the following Java code is run? - Compiler error - Exception - This final exam is very easy will be displayed - This final exam is easy will be displayedarrow_forwardCan someone help rewrite the main method without the use of try, catch and input stream please?arrow_forward
- Solve In Python Provide Screenshots of Input and Ouput For this lab, you will modify your Lab 7 to make several improvements to the getter/settermethods. Instead of having them return True/False, modify each one to throw an exception ifinvalid data is passed.Modify these methods in the following way:• add_hours()o If the number of hours being added is less than 0, throw an exception• add_sales()o If the amount of sales being added is less than 0, throw an exception• set_employee_number()o The employee number must be an integer. If the given input is not an integer,throw an exception• set_office_number()o If the office number given is less than 100 or greater than 500, throw anexception.• set_name()o If the given name is empty, throw an exceptiono Any of the following characters should be removed from the name: ‘_’, ‘.’, ‘-‘ (Underscore, period, and dash)• set_birthdate()o If the given value for the month is less than 1 or greater than 12, throw anexceptiono If the given value for the…arrow_forwardPlease come up with some scenario where mocking is required, write the Python class, and the Python unit test that included the patch that enables the mocking. You may patch a single value using return_value, or multiple values with side_effect. from territory import Territoryfrom army import Armyfrom territory_test import TestDoBattle t = Territory(10, Army())t.do_battle(Army())print(t.curr_territory) unittest.main() ----- class Army:def get_curr_strength(self):return 0 # pretend there is code here that returns an actual value ------ from army import Armyclass Territory:def __init__(self, initial_territory, this_army):self.curr_territory = initial_territoryself.this_army = this_army def do_battle(self, enemy_army):self.curr_territory += self.this_army.get_curr_strength() - \enemy_army.get_curr_strength()------ something similar to this code: from territory import Territoryfrom army import Armyimport unittestfrom unittest.mock import patch class…arrow_forwardPlease predict all output for each of the following, and then verify your answer. Provide your reasoning for the results. If there is an error, please indicate the specific syntax or runtime exception to the best of your ability. If there is a never ending recursion, write “Never ending recursion” in the output, and explain why.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education