Java Programming - Exceptions - Discussion

Discussion Forum : Exceptions - Pointing out the correct statements (Q.No. 4)
4.
System.out.print("Start ");
try 
{
    System.out.print("Hello world");
    throw new FileNotFoundException();
}
System.out.print(" Catch Here "); /* Line 7 */
catch(EOFException e) 
{
    System.out.print("End of file exception");
}
catch(FileNotFoundException e) 
{
    System.out.print("File not found");
}
and given that EOFException and FileNotFoundException are both subclasses of IOException, and further assuming this block of code is placed into a class, which statement is most true concerning this code?
The code will not compile.
Code output: Start Hello world File Not Found.
Code output: Start Hello world End of file exception.
Code output: Start Hello world Catch Here File not found.
Answer: Option
Explanation:

Line 7 will cause a compiler error. The only legal statements after try blocks are either catch or finally statements.

Option B, C, and D are incorrect based on the program logic described above. If line 7 was removed, the code would compile and the correct answer would be Option B.

Discussion:
8 comments Page 1 of 1.

Ranjeet said:   5 years ago
I didn't get exact it. Please explain me.

Tohid said:   6 years ago
Can we use throw in try block?

Prabhat mishra said:   6 years ago
Please give the detailed answer.

Groumpf said:   7 years ago
I agree with Kalman.

"Assuming this block of code is placed into a class" is confusing.

I answered compilation error because this block cannot be put in a class.

Kalman said:   7 years ago
"Assuming this block of code is placed into a class".

Indeed it must be placed into a method to work.

Mayuri said:   1 decade ago
The first catch block causes compiler error as we are throwing FileNotFoundException it will not catch EOFException.

Tiago said:   1 decade ago
The first catch block will also cause compiler error. Because it will never be executed.

NaGeNdRa said:   1 decade ago
Yes, After try block the legal statement is catch or finally. !

Post your comments here:

Your comments will be displayed after verification.