Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
Question
Book Icon
Chapter 12.8, Problem 12.8.1CP
Program Plan Intro

Given code:

//Class definition

public class ChainedExcptionDemo

{

    //Main method

    public static void main (String[ ] args)

    {

        //Try block

        try {

            //Call method() method

            method1() ;

        }

        //Catch block

        catch (Exception ex) {

            // printStackTrace() method displays the

//exception name and description and also //print line name of error

            ex.printStackTrace();

        }

    }

    //Method definition

    public static void method1() throws Exception

    {

        //In try block

        try {

            //Call method() method

            method2() ;

        }

        //Catch block

        catch (Exception ex) {

            //Line16 statement to replace

            //Throw the checked exception

throw new Exception ("New info from method1", ex);

        }

    }

    //Method definition

    public static void method2( ) throws Exception

    {

        //Throw the checked exception

        throw new Exception( "New info from method2" );

    }

}

Blurred answer
Students have asked these similar questions
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);    }}
Consider the following test method code fragment, which tests whether a method given invalid input produces the expected exception in response. On which of the following lines should the test return false, indicating that the test has failed? There may be more than one correct answer! try { methodCall(invalidInput); // 1. line after method call } catch (IllegalArgumentException e) { // 2. correct } catch (Exception e) { // 3. incorrect } Group of answer choices 1. line after method call 2. correct 3. incorrect
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.

Chapter 12 Solutions

Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)

Ch. 12.4 - Prob. 12.4.3CPCh. 12.4 - Prob. 12.4.4CPCh. 12.4 - Prob. 12.4.5CPCh. 12.4 - Prob. 12.4.6CPCh. 12.4 - What is displayed when running the following...Ch. 12.4 - Prob. 12.4.8CPCh. 12.4 - What does the method getMessage() do?Ch. 12.4 - What does the method printStackTrace() do?Ch. 12.4 - Prob. 12.4.11CPCh. 12.4 - Prob. 12.4.12CPCh. 12.5 - Prob. 12.5.1CPCh. 12.6 - Prob. 12.6.1CPCh. 12.7 - Prob. 12.7.1CPCh. 12.8 - Prob. 12.8.1CPCh. 12.9 - Prob. 12.9.1CPCh. 12.9 - Prob. 12.9.2CPCh. 12.10 - What is wrong about creating a File object using...Ch. 12.10 - How do you check whether a file already exists?...Ch. 12.10 - Can you use the File class for I/O? Does creating...Ch. 12.11 - Prob. 12.11.1CPCh. 12.11 - Prob. 12.11.2CPCh. 12.11 - Prob. 12.11.3CPCh. 12.11 - Prob. 12.11.4CPCh. 12.11 - What will happen if you attempt to create a...Ch. 12.11 - Prob. 12.11.6CPCh. 12.11 - Suppose you enter 45 57, 8 789, then press the...Ch. 12.11 - Prob. 12.11.8CPCh. 12.12 - How do you create a Scanner object for reading...Ch. 12.13 - Prob. 12.13.1CPCh. 12.13 - Simplify the code in lines 20-28 as follows: 1....Ch. 12 - Prob. 12.1PECh. 12 - (InputMismatchException) Write a program that...Ch. 12 - (ArrayIndexOutOfBoundsException) Write a program...Ch. 12 - (IllegalArgumentException) Modify the Loan class...Ch. 12 - (IllegalTriangleException) Programming Exercise...Ch. 12 - (NumberFormatException) Listing 6.8 implements the...Ch. 12 - Prob. 12.7PECh. 12 - Prob. 12.8PECh. 12 - Prob. 12.9PECh. 12 - Prob. 12.10PECh. 12 - Prob. 12.11PECh. 12 - (Reformat Java source code) Write a program that...Ch. 12 - (Count characters, words, and lines in a file)...Ch. 12 - (Process scores in a text file) Suppose a text...Ch. 12 - (Write/read data) Write a program to create a file...Ch. 12 - Prob. 12.16PECh. 12 - (Game: hangman) Rewrite Programming Exercise 7.35....Ch. 12 - Prob. 12.18PECh. 12 - (Count words) Write a program that counts the...Ch. 12 - Prob. 12.20PECh. 12 - (Data sorted?) Write a program that reads the...Ch. 12 - Prob. 12.22PECh. 12 - (Process scores in a text file on the Web) Suppose...Ch. 12 - (Create large dataset) Create a data file with...Ch. 12 - (Create a directory) Write a program that prompts...Ch. 12 - Prob. 12.26PECh. 12 - (Replace words) Suppose you have a lot of files in...Ch. 12 - (Rename files) Suppose you have a lot of files in...Ch. 12 - (Rename files) Suppose you have several files in a...Ch. 12 - (Occurrences of each letter) Write a program that...Ch. 12 - (Baby name popularity ranking) The popularity...Ch. 12 - (Ranking summary) Write a program that uses the...
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning