Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 9.2, Problem 18STQ
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.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
1.public class Test{
public static void main(String args[]){
Person p = null;
try{
}
p.talk();
} catch (NullPointerException e) {
System.out.print("There is a NullPointerException.");
}
catch(Exception e) {
System.out.print("There is an Exception.");
}
System.out.print("Everything went fine.");
}
what will be the result if you run this program?
O There is a NullPointerException. Everything went fine.
O There is a NullPointerException.
There is a NullPointerException. There is an Exception.
This code will not compile, because in Java there are no pointers.
Suppose the exception class CrazyException is defined as follows: public class CrazyException extends Exception { public CrazyException() { super("Crazy exception thrown!"); System.out.println("Wow, Crazy exception thrown!"); } public CrazyException(String message) { super(message); System.out.println("Wow, crazy exception thrown with "+ "an argument!"); } public void crazyMethod() { System.out.println("Message is " + getMessage()); } } What output would be produced by the following unlikely code? CrazyException exceptionObject = new CrazyException(); System.out.println(exceptionObject.getMessage()); exceptionObject.crazyMethod();
Suppose the exception class TestException is defined as follows:public class TestException extends Exception{public TestException(){super("Test Exception thrown!");System.out.println("Test exception thrown!!");}public TestException(String message){super(message);System.out.println("Test exception thrown with an argument!");}public void testMethod(){System.out.println("Message is " + getMessage());}}What output would be produced by the following code (which is just anexercise and not likely to occur in a program)?TestException exceptionObject = new TestException();System.out.println(exceptionObject.getMessage());exceptionObject.testMethod();The class TestException is on the website that comes with this text.
Chapter 9 Solutions
Java: An Introduction to Problem Solving and Programming (8th 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 - 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
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
- JAVA programming languagearrow_forwardAssume that the following code segment is executed. Which may occur when the run method of the program executes?arrow_forward1.class Nothing {public static void main(String args[]){int x = 0;int y = 20;int fraction = y/x;System.out.println("End Of Main");}}1. If there will occur exception, write the code that will handle the exception.arrow_forward
- Javaarrow_forwardTime Converter 1. Write a program that converts dates from a numerical month-day format to alphabetic month-day format. IE 1/31 or 01/31 would have an output of January 31. 1. User enters the month and day as a single string. It is then converted. (10%) 2. Create 2 exception classes (20%) 1. MonthException thrown for invalid months 2. DayException-thrown for invalid days for the given month 3. You can assume Feb is always 28 days 2. This should run in a for loop and end when a user is done entering dates (70%)arrow_forwardJAVA EXCEPTION HANDLING: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. For example: Input Result 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 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…arrow_forward
- Question 3 } public class WeekDay { static void daysFromSunday(int day) { String dayName = ""; try { } } if (day > 7) { throw new Exception("Invalid Day! Too high!"); } else if(day < 1){ throw new Exception("Invalid Day! Too low!"); } switch(day){ case 1: dayName="Sun"; break; case 2: dayName="Mon"; break; case 3: dayName="Tue"; break; case 4: dayName="Wed"; break; } ▬▬ } case 5: dayName="Thu"; Dreak; case 5: dayName="Thu"; break; case 6: dayName="Fri"; break; default: dayName="Sat"; System.out.println(dayName); } catch (Exception excpt) { System.out.println("Oops"); System.out.println(excpt.getMessage()); }finally{ System.out.println("Done!"); public static void main(String[] args) { daysFromSunday(10); daysFromSunday(3); daysFromSunday(0); OOops Invalid Day! Too high! Tue Oops Invalid Day! Too low! OOops Invalid Day! Too high! Done! Tue Done! Oops Invalid Day! Too low! Done! O Oops Invalid Day! Too high! Done! Oops Invalid Day! Too high! Done! Oops Invalid Day! Too low! Done! O Oops…arrow_forwardjavaarrow_forwardDefine an exception class called CoreBreachException. The class should have a default constructor. If an exception is thrown using zero-argument constructor, getMessage should return "Cor Breach Evacuation Ship!" The class should also define a constructor having a single parameter of type String. If an exception is thrown using constructor, getMessage should return the value that was used as an argument to constructor.arrow_forward
- Defined Exception Handling Exception Handling is used to overcome exceptions that take place during programs’ execution in a systematic manner. For instance, dividing a number by ‘0’ is a mathematical error (not defined). As you learned in class, you can use exception handling to overcome such operations. If you write a code without using exception handling, then the output of division by zero will be shown as infinity which cannot be further processed. Consider the following code: float Division(float num, float den) {return (num / den);} int main() { float numerator = 12.5; float denominator = 0; float result; result = Division(numerator, denominator); cout << "The quotient of 12.5/0 is " << result << endl; }arrow_forwardDefine an exception class called CoreBreachException. The class should have a default constructor. If an exception is thrown using this zero- argument constructor, getMessage should return "Core Breach! Evacuate Ship!" The class should also define a constructor having a single parameter of type String. If an exception is thrown using this constructor, getMessage should return the value that was used as an argument to the constructor.arrow_forwardJAVA programming languagearrow_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 LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT