Multiple choice technology programming languages

What is the result when you compile and run the following code? public class ThrowsDemo { static void throwMethod() { System.out.println("Inside throwMethod."); throw new IllegalAccessException("demo"); } public static void main(String args[]) { try { throwMethod(); } catch (IllegalAccessException e) { System.out.println("Caught " + e); } } }

  1. Compile successfully, nothing is printed

  2. Runtime error

  3. Compilation error

  4. Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: demo

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

IllegalAccessException is a checked exception in Java. Any method throwing a checked exception must either declare it with a throws clause or catch it - throwMethod does neither, causing compilation error.

AI explanation

IllegalAccessException is a checked exception, and Java requires that any checked exception thrown inside a method either be caught or declared in that method's throws clause. Since throwMethod() throws it without declaring throws IllegalAccessException, the code fails to compile — the surrounding try/catch in main doesn't help because the compiler flags the error at the point where throwMethod itself is defined.