nerdexam
Oracle

1Z0-808 · Question #43

Given the code fragment: ``java 1. class X { 2. public void printFileContent () { 3. / code goes here / 4. throw new IOException(); 5. } 6. } 7. public class Test { 8. public static void…

The correct answer is A. Replace line 8 with public static void main(String[] args) throws Exception C. Replace line 2 with public void printFileContent () throws IOException. IOException is a checked exception, meaning Java requires it to be either declared in the method signature or caught - it can't silently propagate. Option C fixes the root cause by adding throws IOException to printFileContent(), telling callers the method may throw it. Option…

Handling Exceptions

Question

Given the code fragment:
1. class X {
2. public void printFileContent () {
3. /* code goes here */
4. throw new IOException();
5. }
6. }
7. public class Test {
8. public static void main(String[] args) {
9. X xobj = new X();
10. xobj.printFileContent ();
11. }
12. }
Which two modifications should you make so that the code compiles successfully?

Options

  • AReplace line 8 with public static void main(String[] args) throws Exception
  • BReplace line 10 with:
    try {
     xobj.printFileContent();
    } catch (Exception e) {
    }
    catch (IOException e) {
    }
    
  • CReplace line 2 with public void printFileContent () throws IOException
  • DReplace line 4 with throw IOException("Exception raised")
  • EAt line 11, insert throw new IOException();

How the community answered

(26 responses)
  • A
    92% (24)
  • B
    4% (1)
  • E
    4% (1)

Explanation

IOException is a checked exception, meaning Java requires it to be either declared in the method signature or caught - it can't silently propagate. Option C fixes the root cause by adding throws IOException to printFileContent(), telling callers the method may throw it. Option A fixes the call site in main() by declaring throws Exception (which covers IOException as a subclass), satisfying Java's requirement to handle or re-declare the propagating exception.

Why the distractors fail:

  • B is invalid because catch (Exception e) appears before catch (IOException e) - since IOException is a subclass of Exception, the second block is unreachable, which is a compile error in Java.
  • D is a syntax error - throw requires the new keyword to instantiate the exception object: throw new IOException(...).
  • E adds an unrelated throw after the method call, which would itself be unreachable (dead code after a throwing call) and doesn't resolve the original uncaught checked exception issue.

Memory tip: Think "declare or catch, or code won't pass" - every checked exception must either be caught with try-catch at the call site or declared with throws all the way up the call chain. When you see throws missing from a method that throws a checked exception, that's your first fix; then follow the chain upward.

Topics

#Checked Exceptions#throws Declaration#Exception Propagation#Method Signatures

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice