Multiple choice java core java

What happens in a method if an unchecked exception is thrown in a try block and there is no matching catch block?

  1. 1.program ignores exceptn

  2. 2.program halts immediately

  3. 3.Program will not compile

  4. 4.None of the above

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

When an unchecked exception (like RuntimeException) is thrown and not caught by any catch block, it propagates up the call stack. If no method in the call stack handles it, the JVM terminates the program and prints the stack trace. The program compiles successfully because unchecked exceptions don't require mandatory catch blocks - only checked exceptions do. Option A is wrong because the exception is not ignored. Option C is wrong because the absence of a catch block for unchecked exceptions is a compile-time legal scenario.

AI explanation

To answer this question, you need to understand how exception handling works in Java.

When an unchecked exception is thrown in a try block and there is no matching catch block, the program will halt immediately.

Option A) 1. The program ignores the exception - This option is incorrect because when an exception is thrown and there is no matching catch block, the program does not ignore the exception. Instead, it halts immediately.

Option B) 2. The program halts immediately - This option is correct. When an unchecked exception is thrown and there is no matching catch block, the program will halt immediately.

Option C) 3. The program will not compile - This option is incorrect. The program will still compile even if there is no catch block for an unchecked exception.

Option D) 4. None of the above - This option is incorrect. Option B is the correct answer.

The correct answer is B) 2. The program halts immediately because there is no matching catch block for the unchecked exception.