Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Review: 1 3 5 6 9 11 12 15 Find The Error: 1 catch (FileNotFoundException e) { System.out.println(File not found.); } try { File file

  • Review:
    • 1
    • 3
    • 5
    • 6
    • 9
    • 11
    • 12
    • 15
  • Find The Error:
    • 1

catch (FileNotFoundException e)

{

System.out.println("File not found.");

}

try

{

File file = new File("MyFi;le.txt");

Scanner inputFile = new Scanner(file);

}

    • 3

try

{

number = Integer.parseInt(str);

}

catch (Exception e)

{

System.out.println(e.getMessage());

}

catch (IllegalArgumentException e)

{

System.out.println("Bad number format.");

}

catch (NumberFormatException e)

{

System.out.println(str + " is not a number.");

}

  • Workbench
    • 2
      • Look at the following program and tell what the program will output when run

public class ExceptionTest

{

public static void main(String[] args)

{

int number;

String str;

try

{

str = "xyz";

number = Integer.parseInt(str);

System.out.println("A";

}

catch(NumberFormatException e)

{

System.ouyt.priuntln("B");

}

catch(IllegalArgumentException e)

{

System.out.println("C");

}

finally

{

System.out.print("D");

}

System.out.print("E");

}

}

  • Program Challenge
    • 1 TestScores Class
      • Write a class named TestScores.
      • The class constructor should accept an array of test scores as its argument.
      • The class should have a method that returns the average of the test score.
      • If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program
    • 2 TestScores Class Custom Exception
      • Writ an exception class named InvalidTestScore.
      • Modify the TestScores class you wrote in Programming Challenge 1 so it throws an InvalidTestScore exception if any of the test scores in the array are invalid.
image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
System out printing a" catchIllegalArgumentException al System out printing"e- 1: System out print In("D") Look at the following program and tell what the program will output when run public class ExceptionTest public static void main(String args) int number String str try str = "xyz number = Integer parseInt (str) System out print in( "A") catch( Number FormatException e) System out printin("B" )Review Questions and Exercises 737 12 . These are exceptions that inherit from the Error class or the RuntimeException class. a. unrecoverable exceptions b. unchecked exceptions c. recoverable exceptions d. checked exceptions 3 All exceptions that do not inherit from the Error class or the RuntimeException class are a. unrecoverable exceptions b. unchecked exceptions c. recoverable exceptions d. checked exceptions a. throws clause 14. This informs the compiler of the exceptions that could get thrown from a method. b. parameter list c. catch clause d. method return type 15. You use this statement to manually throw an exception. a. try b. generate c. throw d. System . exit (0) 16. This is the process of converting an object to a series of bytes that represent the object's data. a. Serialization b. Deserialization c. Dynamic conversion d. Casting 17. True or False: You are not required to catch exceptions that inherit from the RuntimeException class. 18. True or False: When an exception is thrown by code inside a try block, all of the state- ments in the try block are always executed. 19. True or False: 10Exception serves as a superclass for exceptions related to program- ming errors, such as an out-of-bounds array subscript. 20. True or False: You cannot have more than one catch clause per try statement. 21. True or False: When an exception is thrown, the JVM searches the try statement's catch clauses from top to bottom, and passes control of the program to the first catch clause with a parameter that is compatible with the exception. 22. True or False: Not including polymorphic references, a try statement can have only one catch clause for each specific type of exception. 23. True or False: When in the same try statement you are handling multiple except tions, and some of the exceptions are related to each other through inheritance, you should handle the more general exception classes before the more specialized excep tion classes. 24. True or False: The throws clause causes an exception to be thrown.Chapter 10 Exceptions and Advanced File 1/O 4 . All exception classes inherit from this class. a. Error b. RuntimeException C. JavaException d. Throwable . FileNot FoundException inherits from a. Error b. TOException C. JavaException d. FileException 6. You can think of this code as being "protected" because the application will not hal if it throws an exception. a. try block b. catch block c. finally block d. protected block 7. This method can be used to retrieve the error message from an exception object, a. errorMessage b. errorString c. getError d. getMessage 8. The numeric wrapper classes' "parse" methods all throw an exception of this type. a. ParseException b. NumberFormatException c. IOException d. BadNumberException 9. This is one or more statements that are always executed after the try block has executed, and after any catch blocks have executed if an exception was thrown. a. try block b. catch block c. finally block d. protected block 10. This is an internal list of all the methods that are currently executing. a. invocation list b. call stack c. call list d. list trace 11. This method can be called from any exception object, and it shows the chain of methods that were called when the exception was thrown. a. print InvocationList b. printCallStack c. printStackTrace rint Calllister 10 Exceptions and Advanced File 1/O Find the Error Find the error in each of the following code segments: 1. catch (FileNotFoundException e) System. out . printIn( "File not found. ") ; try File file = new File ("MyFile. txt") ; Scanner inputFile = new Scanner (file) ; . / / Assume inputFile references a Scanner object. try input = inputFile . nextInt( ) ; finally inputFile . close () ; catch (InputMismatchException e)Review Questions and Exercises 735 . Getting the try, catch, and finally clauses out of order. In a try statement, the try finally clause. clause must appear first, followed by all of the catch clauses, followed by the optional Writing two catch clauses that handle the same exception in the same try statement. statement. You cannot have more than one catch clause per exception type in the same try When catching multiple exceptions that are related to one another through inheri- tance, listing the more general exceptions first. When in the same try statement you are handling multiple exceptions, and some of the exceptions are related to each other through inheritance, you should handle the more specialized exception classes before the more general exception classes. Otherwise, an error will occur because the com- piler thinks you are handling the same exception more than once. Forgetting to write a throws clause on a method that can throw a checked exception but does not handle the exception. If a method is capable of throwing a checked exception but does not handle the exception, it must have a throws clause in its header that specifies the exception. Calling a method but not handling an exception that it might throw. You must either handle all of the checked exceptions that a method can throw, or list them in the call- ing method's throws clause. . In a custom exception class, forgetting to pass an error message to the superclass's constructor. If you do not pass an error message to the superclass's constructor, the exception object will have a null error message. . Serializing an object with members that are not serializable. If a class has fields that are objects of other classes, those classes must implement the Serializable interface in order to be serialized. Review Questions and Exercises Multiple Choice and True/False 1. When an exception is generated, it is said to have been a. built b. thrown c. caught d. killed 2. This is a section of code that gracefully responds to exceptions. a. exception generator b. exception manipulator c. exception handler d. exception monitor 3. If your code does not handle an exception when it is thrown, it is dealt with by this. a. default exception handler b. the operating system c. system debugger d. default exception generator

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions