Explanation of Solution
Given program:
IntException.java:
//Define a class
public class IntException extends Exception
{
private int intMessage;
//Default constructor
public IntException()
{
//Call the parent class by passing the message
super("IntException thrown!");
}
//Parameterized constructor
public IntException(String message)
{
//Call the parent class by passing the message
super(message + " " + message);
}
//Parameterized constructor
public IntException(int number)
{
//Call the parent class by passing the message
super("IntException thrown!");
//Assign the value
intMessage = number;
}
//Method to get the number
public int getNumber()
{
//Return the value
return intMessage;
}
}
Main...
Want to see the full answer?
Check out a sample textbook solutionChapter 9 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
- Define 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_forwardJavaarrow_forwardQuestion 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_forward
- 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.arrow_forwardvoid foo () { try { throw new Exception1 (); print (" A "); throw new Exception2 (); print (" B "); } catch ( Exception1 e1 ) { print " handler1 "; } print (" C "); throw new Exception2 (); } void main () { try { try { foo (); print (" D "); } catch ( Exception1 e1 ) { print " handler2 "; } print (" E "); } catch ( Exception2 e2 ) { print " handler3 "; } } Write down the output of the program and justify why it is the output. Instead of the “replacement” semantics of exception handling used in modern languages, a very early design of exception handling introduced in the PL/I language uses a “binding” semantics. In particular, the design dynamically tracks a sequence of “catch” blocks that are currently active; a catch block is active whenever the corresponding try block is active. What will be the output of the following program if the language uses the “binding” semantics? Give the reasoning for your answer.arrow_forwardjavaarrow_forward
- Define 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_forwardDevelop an exception class named InvalidMonthException that extends the Exception class. Instances of the class will be thrown based on the following conditions: The value for a month number is not between 1 and 12 The value for a month name is not January, February, March, … December Develop a class named Month. The class should define an integer field named monthNumber that holds the number of a month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods: A no-argument constructor that sets the monthNumberto 1. An overloaded constructor that accepts the number of the month as an argument. The constructor should set the monthNumberfield to the parameter value if the parameter contains the value 1 – 12. Otherwise, throw an InvalidMonthException exception back to the caller. The exception should note that the month number was incorrect. An overloaded constructor that accepts a string containing the name of the month,…arrow_forwardDefined 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_forward
- Java Exception method withdraw throws an exception if amount is greater than balance. For example: Test Result Account account = new Account("Acct-001","Juan dela Cruz", 5000.0); account.withdraw(5500.0); System.out.println("Balance: "+account.getBalance()); Insufficient: Insufficient funds. Balance: 5000.0 Account account = new Account("Acct-001","Juan dela Cruz", 5000.0); account.withdraw(500.0); System.out.println("Balance: "+account.getBalance()); Balance: 4500.0arrow_forwarduse C++ programing language Write a program that prompts the user to enter a person's date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Similar conventions for the invalid values of month and year. (Note that your program must handle a leap year.)arrow_forwardSubject: 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_forward
- 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 LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT