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.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
What is the difference between using @Test(expected = TypeOfException) and using try/catch when testing if a method or constructor properly returns an exception? A. They do the same thing.   B. @Test(expected = ...) is the proper way to unit test in java, and try/catch should only be used when unit testing in Python.   C. @Test(expected = ...) will give a passing test as soon as one exception of the proper type is thrown and then stop the test, but using try/catch will allow you to test many examples in the same test and will only stop after running all examples or reaching a fail() or an assert statement that fails.   D. We should never use try/catch when unit testing, otherwise we will catch the exception and then won't know if it is actually thrown.
*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…
Let's revisit chapter 5 example, but this time, no method can throw any exception out of the method and if an exception was detected then you will have to throw your own custom exception to let the user know what happened.   public class Chapter11Demo{ public static void main(java.lang.String[] args) {method1();}private static void method1(){ method2();}private static void method2(){ method3();}private static void method3(){ method4();}private static void method4(){ method5();}private static void method5(){ method6();}private static void method6(){       java.io.File in=new java.io.File("somefile.txt");       java.io.File out=new java.io.File("somefile2.txt");       java.util.Scanner inFile=new java.util.Scanner(in);       java.io.FileWriter outFile=new java.io.FileWriter(out);    }}

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