Consider the following code snippet: PrintWriter outputFile = new Printwriter (f
ID: 3830647 • Letter: C
Question
Consider the following code snippet: PrintWriter outputFile = new Printwriter (filename); write Data (output File); output File.close (); How can the program ensure that the file will be closed if an exception occurs on the writeData call? Explain your answer. A Quiz class contains an array of Question objects. This is best described as an example of... Explain. You are designing a software solution for an automobile rental company. You have decided that the following nouns apply to the requirements: Auto, Customer, Address, Rental Contract, Mileage, Rental Date, Daily Rate, Total. Which of these should be represented as classes? Explain your answer.Explanation / Answer
PrintWriter outputFile = null;
try {
outputFile = new PrintWriter(filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
writeData(outputFile);
} catch (Exception e) {
// if you have somelogic to recover from exception
}
finally
{
outputFile.close();
}
This would be right way to write this code, it should first check for file existance.
Now about how to ensure file is getting closes, this can be done by a finally block which will always get executed once we are out of either try or catch block. IN finally we can put our clean up logic which is file closing here.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.