Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 9.3, Problem 26STQ
Program Plan Intro
Exception:
- It is nothing but an undesirable or unexpected event that occurs at run time which interrupts the normal flow of the
program ’s commands. - Exception handling is a
mechanism that handle runtime errors like “ClassNotFoundException”, “SQLException”, and so on. - An advantage of exception handling is to maintain a flow of the program.
- An exception can be handled using “try” and “catch” block.
Types of exceptions:
There are two types of exceptions. They are:
- Checked Exception:
- A class that inherits a “Throwable” class except “RunTimeException” and “Error” are called as checked exceptions.
- Example: “IOException”
- Unchecked Exception:
- A class that inherits a “RunTimeException” class is called as unchecked exceptions.
- Example: “ArrayIndexOutOfBoundException”
“throws” clause:
- This clause is used to declare the exception. This provides information to the programmer that the program might produce an exception so it is better to give the exception handling code which maintains the normal flow of the program.
- It is usually given in the method header.
Syntax:
Method_Name throws ExceptionName
{
//statements
}
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
please solve this important, thanks.
What is exception propagation? Give an example of a class that contains at least two methods, in which one method calls another. Ensure that the subordinate method will call a predefined Java method that can throw a checked exception. The subordinate method should not catch the exception. Explain how exception propagation will occur in your example.
Write a class TimeOfDay that uses the exception classes defined in the previous exercise. Give it a method setTimeTo(timeString) that changes the time if timeString corresponds to a valid time of day. If not, it should throw an exception of the appropriate type. [Java]
Chapter 9 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
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 - Rewrite the class ColorDemo in Listing 9.13 so...Ch. 9.4 - Prob. 37STQCh. 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 - Prob. 9PPCh. 9 - Suppose that you are in change of customer service...Ch. 9 - Write an application that implements a trip-time...
Knowledge Booster
Similar questions
- Java Foundations : Please provide code and explain. Thank you. Write the code segments in main( ) and in methodA( ) for the following scenario: main( ) passes methodA an int variable called a. methodA generates an exception, but does not deal with it, if a is not between 1 and 100.arrow_forward5. Write a JAVA program to define a static method checkPT() to receive 3 positive integer parameters and checks whether they form Pythagorean triplet. If yes, display appropriate message, otherwise, throw custom exception called InvalidPythagoreanTripletException. The checkPT() method does not handle the exception thrown. Write main method in the same class to read 3 integers and make call to checkPT() method infinitely. The InvalidPythagoreanTripletException exception object. Use throw, throws, try and catch keywords appropriately in the program. Also illustrate re-throwing an exception in the main method handles the exception by displaying the program.arrow_forwardUse "Java Programme "arrow_forward
- Write a program that shows a constructor passing information about constructor failure to an exception handler. Define class SomeException, which throws an Exception in the constructor. Your program should try to create an object of type SomeException and catch the exception that is thrown from the constructor.arrow_forwardjavaarrow_forwardpublic class Test { public static void main (String [] args) { Object circlel = new Circle (); Object circle2 = new Circle (); System.out.println(circlel.equals (circle2)); class Circle { double radius; class Circle { double radius; public boolean equals (Circle circle) { public boolean equals (Object o) { return this.radius = ( (Circle)o).radius; return this.radius = circle.radius;arrow_forward
- Suppose there exists a catch handler that matches the exception object type perfectly. Under what circumstances may exception objects of this type be treated differently?arrow_forwardDefine a public static method called squareArea that takes one double argument called length,which is the side length of a square. This method will return the area of the square (side * side), which is a double. Do not define a class. You may only use techniques and data structures that have already been covered in this class. Do not include any exception handling. For example, squareArea(5.0) should return 25.0.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_forward
- In C++ You have a class called Fraction, which uses the heap. The constructor for the class is declared as: Fraction::Fraction(); Implement an exception handling design in the parameterized constructor to catch zeros in the denominator. Show the new exception class you will need to put in the header (specification) file. Show the definition for your parameterized constructor that throws the exception if the denominator is zero. Finally, show how to use the try-catch statement in the driver program when instatiating a new paramerized Fraction object. You do not need to show entire programs, only the requested lines of source codearrow_forwardthis is a java theory question write the answer carefully and do not plagarize or copy from internet please write in simple words so that i can understandarrow_forwardIn Java. Define the class InvalidSideException, which inherits from the Exception class. Also define a Square class, which has one method variable -- an int describing the side length. The constructor of the Square class should take one argument, an int meant to initialize the side length; however, if the argument is not greater than 0, the constructor should throw an InvalidSideError. The Square class should also have a method getArea(), which returns the area of the square.Create a Driver class with a main method to test your classes. Your program should prompt the user to enter a value for the side length, and then create a Square object with that side length. If the side length is valid, the program should print the area of the square. Otherwise, it should catch the InvalidExceptionError, print "Side length must be greater than 0.", and terminate the program. The words for output is highlighted in grey and yellow in the blue box. Class should be named Driver, as shown in blue box…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
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