You are here
Home >

Which of the following statements is true about exception handling in Java?

A) A try block can have many catch blocks but only one finally block

B) A try block can have many catch blocks and many finally blocks

C) A try block must have one finally block for each catch block

D) A try block must have at least one catch block to have a finally block

Ans. A) A try block can have many catch blocks but only one finally block

Explanation:

In Java, the exception handling mechanism uses try, catch, and finally blocks to manage exceptions that occur during program execution. Option A is correct because a try block can indeed have multiple catch blocks to handle different types of exceptions. However, it can only have one finally block. The finally block is used to execute code that must run regardless of whether an exception occurred or not, such as releasing resources.

Why Other Options Are Not Correct:

Here’s why the other options are not correct:

  • Option B is incorrect because a try block can only have one finally block. You cannot have multiple finally blocks.
  • Option C is incorrect because a try block does not need to have a finally block for each catch block. A finally block, if present, is singular and associated with the try block, regardless of the number of catch blocks.
  • Option D is incorrect because a finally block can exist without any catch blocks. It is not mandatory to have at least one catch block to include a finally block.

Top