Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 9.3, Problem 27STQ

Explanation of Solution

Given program segment:

//Try block

try

{

    //Assign a value to the variable

    int n = 7;

    //Checking if value is greater than 0

    if(n > 0)

        //Throwing a predefined exception

        throw new Exception();

    //Checking if n is lesser than 0

    else if (n < 0)

        //Throwing user-defined exception

        throw new NegativeNumberException();

    //Else

    else

        //Print Hello

        System.out.println("Hello!");

}

//Catch the user-defined exception

catch (NegativeNumberException e)

{

    //Print the message

    System...

Blurred answer
Students have asked these similar questions
*This is a handout from my class that I'm having trouble with I tried YouTube videos and I'm still lost please explain the answers as simply as you can and thank you.* A. Look over the following code fragment, give the output: try { method(); System.out.println("After the method call"); } catch (Exception ex) { System.out.println("Exception in main"); } catch (RuntimeException ex) { System.out.println("RuntimeException in main"); } static void method() throws Exception { System.out.println(1 / 0); }   B. Write the code to load an array. Use simple I/O (system.out.print, etc.) to get integer values from the user and load the array (loop?). Your code has the potential to generate 2 exceptions: 1) InputMismatch (research this) and 2) IllegalValueException (assume this exists). If the 1st occurs, change the input to be 99, warn the user and continue. The 2nd exception will occur when the user enters a 0; if this occurs, notify the user that a 0 has been entered, and change the input to…
ANSWER IN JAVA Standard Output: Enter·side·length·of·square:Side·length·must·be·greater·than·0.↵ 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.
Using JavaFX, create a simple calculator application, like the following: The operators is ( + : " addition process" , -:subtraction process", *:"multiplication process" , /:"division process" , C : "Clear" , H: "History"); Validate that the first number and second is a numeric value if it is not throwing an exception. When clicking the operators (+, -, *, /): the result will display at the bottom, and stored in history. When clicking C, clear the first, second number and the history. When clicking H, the history will display at the bottom. When the user click / and the second number is 0 throw an arithmetic exception.

Chapter 9 Solutions

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

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education