Question
import java.io.*;
public class MyProgram
{
public static void main(String args[])
{
FileOutputStream out = null;
try
{
out = new FileOutputStream("test.txt");
out.write(122);
}
catch(IOException io)
{
System.out.println("IO Error.");
}
finally
{
out.close();
}
}
}
import java.io.*;
public class MyProgram
{
public static void main(String args[])
{
FileOutputStream out = null;
try
{
out = new FileOutputStream("test.txt");
out.write(122);
}
catch(IOException io)
{
System.out.println("IO Error.");
}
finally
{
out.close();
}
}
}
and given that all methods of class FileOutputStream, including close(), throw an
IOException, which of these is true?
Answer: Option D
Was this answer helpful ?
Any method (in this case, the main() method) that throws a checked exception (in this case,
out.close() ) must be called within a try clause, or the method must declare that it throws the
exception. Either main() must declare that it throws an exception, or the call to out.close() in
the finally block must fall inside a (in this case nested) try-catch block.
Was this answer helpful ?
Submit Solution