Tutorial 3-Answers
Tutorial 3-Answers
Today we are going to continue exception handling from tutorial 2 and will also learn about file
operations like read. This tutorial will help you understand how to perform file reading with
exception handling.
2. (a) Tut3_01 class should read a file and compute the sum & average of the numbers found in the
file. Using exception handling display appropriate error messages:
Erroneous scenario:
Invalid input
1
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
}
}
2
2. (b) Modify above class to handle non numeric input and continue to read file if numeric data is still
present in the file. At the end, display the count of non-numeric data in file with sum and average of
numeric data as in above question.
Excepted Output:
Sum is: 267.00
Avg. is: 17.80
Non numeric: 4
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
}
}
3
3. Quiz
3.1. What method of an Exception object prints a list of methods that were called before the
exception was thrown?
a. printErrors()
b. getMessage()
c. printStackTrace()
d. traceStack()
Answer: C
a. getError()
b. getMessage()
c. printMessage()
d. traceMessage()
Answer: B
3.3. What happens during execution if a negative value is used for an array index?
a. An IndexOutOfBoundsException is thrown.
b. A NumberFormatException is thrown.
Answer: A
3.4. A method must do either of two things with a checked exception. What are those two things?
b. (1) Handle the exception in a catch{} block, or (2) return the exception to the sender.
c. (1) Handle the exception in a catch{} block, or (2) throw the exception to the method that called
this method.
d. (1) Handle the exception in a catch{} block, or (2) handle the exception in a try{} block,
Answer: C
4
3.5. What is the only type of exception that is NOT checked?
a. Class Exception.
Answer: B
3.6. Say that methodA calls methodB, and methodB calls methodC. MethodC might throw a
NumberFormatException. Can the program be written so that methodA handles the exception?
d. Yes, if the headers for methodC and methodB say ...throws NumberFormatException
Answer: D
3.7. Which of the following is a correct outline of a method that throws an exception when it finds a
problem?
a.
...
if ( problem )
...
b.
...
if ( problem )
Exception("Useful Message");
...
5
}
c.
...
if ( problem )
...
d.
...
if ( problem )
...
Answer: D
3.8. MethodX might encounter an IOException or an AWTException, but handles neither. How
should the header for methodX be written?
Answer: A
3.9. You are writing a program to check people into a hotel. People over 65 get a 10% discount. How
would you write the program?
a. Use an if statement to test for age 65 or greater. If one is detected, throw an exception. A
catch{} block will apply the discount.
6
c. Subtract 65 from the person's age and divide the bill by the result. If an ArithmeticException
exception is thrown, apply the 10% discount in the exception handler.
d. Create an array with 120 elements, one element for each possible hotel guest age. Each element
contains the discount rate for that age.
Answer: B
3.10. Say that a method catches an IOException in a catch{} block. Is it possible for that block to do
some processing and then throw the same exception to the caller?
c. Yes---as long as the method also has a throws clause for that exception.
Answer: C