Explanation of Solution
Given program:
DoubleException.java:
//Define a class
public class DoubleException extends Exception
{
    //Default constructor
    public DoubleException()
    {
        //Call the method from parent class
        super("Double exception thrown!");
    }
    //Parameterized constructor
    public DoubleException(String message)
    {
        //Call the parent class's method
        super(message + " " + message);
    }
}
Main.java:
//Define the main class
class Main
{
    //Define the main method
    public static void main(String[] args)
    {
        //Try block
        try
        {
            //Print the statement
System.out.println("try block entered:");
            //Declare a variable
            int number = 42;
            //Check if the value is greater than 0
            if(number > 0)
        ...
Want to see the full answer?
Check out a sample textbook solutionChapter 9 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
- in python language , select the good example of the definition of a custom exception type? class InputErrorException: def __init__(self, value): self.value = value return value defInputErrorException:def__init__(self,value):self.value=value  classInputError(Exception):def__init__(self,value):self.value=value  defInputError(Exception):def__init__(self,value):self.value=valuereturnself.valuearrow_forwardSuppose 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_forwardWhat 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.arrow_forward
- JAVA Given a main program that searches for the ID or the name of a student from a text file, complete the findID() and the findName() methods that return the corresponding information of a student. Then, insert a try/catch statement in main() to catch any exceptions thrown by findID() or findName(), and output the exception message. Each line in the text file contains the name and the ID of a student, separated by a space. Method findID() takes two parameters, a student's name and a Scanner object containing the text file's contents. Method findID() returns the ID associated with the student's name if the name is in the file, otherwise the method throws an Exception object with the message "Student ID not found for studentName", where studentName is the name of the student. Method findName() takes two parameters, a student's ID and a Scanner object containing the text file's contents. Method findName() returns the name associated with the student's ID if the ID is in the file,…arrow_forwardCreate user defined Exception Class for Age, if age is zero or less than zero throw exception with message marks should be greater than zero. For this you should take marks input from User, Design window in WPF for Testing User defined exception class? [CLO -3 PLO-2,arrow_forwardOnly new java code can be added after the code given:  The while loop makes multiple attempts to read a nonnegative integer from input into userAge. Use multiple exception handlers to: Catch an InputMismatchException, output "Unexpected input: The UserAge program quits", and assign retry with false. Catch an Exception and output the message of the Exception. End each output with a newline. Ex: If the input is 44, then the output is: Valid input: User's age is 44 Ex: If the input is L, then the output is: Unexpected input: The UserAge program quits Ex: If the input is -65 44, then the output is: User's age must be nonnegative Valid input: User's age is 44    import java.util.Scanner;import java.util.InputMismatchException; public class UserAge {  public static void main(String[] args) {   Scanner scnr = new Scanner(System.in);   int userAge;   boolean retry = true;    while (retry) {     try {      userAge = scnr.nextInt();        if (userAge < 0) {…arrow_forward
- JAVA PROGRAM Probloem: 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. First Example:Input:The quick brown fox jumped over the lazy dogs.Output:The quick brown fox jumped over the lazy dogs. Second 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 brown fox jumped over the lazy dogs.The quick brown…arrow_forwardin java Write a program that reads integers userNum and divNum as input, and output the quotient (userNum divided by divNum). Use a try block to perform the statements. Use a catch block to catch any ArithmeticException and output an exception message with the getMessage() method. Use another catch block to catch any InputMismatchException and output an exception message with the toString() method. Note: ArithmeticException is thrown when a division by zero happens. InputMismatchException is thrown when a user enters a value of different data type than what is defined in the program. Do not include code to throw any exception in the program. Ex: If the input of the program is: 15 3 the output of the program is: 5 Ex: If the input of the program is: 10 0 the output of the program is: Arithmetic Exception: / by zero Ex: If the input of the program is: 15.5 5 the output of the program is: Input Mismatch Exception: java.util.InputMismatchException  LabProgram.java…arrow_forward--------------HW1_P1_INCOMPLETE.JAVA CODE GIVEN____  public class Hw1_part1 { public static void stats(int[] a) {// complete this method}public static void subarray(int[] a, int from, int to) {// error check w/o using Java's exception handlingif (from < 0 || to >= a.length) {System.out.println("Index out of bound");return;}// complete this method}public static void main(String[] args) {// test int[] a = {15, 25, 10, 65, 30, 55, 65};System.out.println("\nGiven array is: " + Arrays.toString(a));stats(a);subarray(a, 1, 4);// test with other arrays} }arrow_forward
- Given the following code, the output is __. try { Integer number = new Integer("1"); System.out.println("An Integer instance."); } catch (Exception e) { System.out.println("An Exception."); }  Group of answer choices A- An Exception. B- 1 C- Error: Exception D- An Integer instance. E- None of the optionsarrow_forwardI have to create a Java program that lets the user interact with it, so they can learn about exceptions. How do I let the user interact with a list of options and direct them to a catch statement? Here is my program so far: import java.util.Scanner;import java.io.*;import java.util.InputMismatchException; public class Exceptions{         public static void main(String[] args)throws IOException{         // declare variables   char option = 'y';       //Arithhmetic & Array TEST   int array[] = {10,20,30,40};   int num1 = 50;   int num2 = 10;          // Setting up scanner to get user data   Scanner exc = new Scanner(System.in);       // Main method (menu, try-catch block)     // all within a loop     try {      }   // I want to learn how an ArrayIndexOutOfBoundsException...     catch(ArrayIndexOutOfBoundsException exception) {      System.out.println("This exception is thrown when the array index that was entered is…arrow_forwardPLZ help with the follwoing IN JAVA Suppose that statement2 causes an exception in the following try-catch block: Will statement3 be executed? If the exception is not caught, will statement4 be executed? If the exception is caught in the catch block, will statement4 be executed?arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning