Java: An Introduction to Problem Solving and Programming (7th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
Author: Walter Savitch
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 9.2, Problem 19STQ

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)

        ...

Blurred answer
Students have asked these similar questions
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.
Write a JAVA PROGRAM to create a user defined exception class. In this, a constructor of InvalidGradeException accepts a string as an argument. This string is passed to constructor of parent class Exception using the super () method. Also the constructor of Exception class can be called without using a parameter and calling super () method is not mandatory.   WRITE JAVA PROGRAM only, and give screenshot of sample run

Chapter 9 Solutions

Java: An Introduction to Problem Solving and Programming (7th Edition)

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning