C#.NET — Exception handling basics: identify the correct facts about try/catch/finally. Which statements are correct? If an exception occurs, the program always terminates abruptly with no chance to recover. Whether or not an exception occurs, the statements in the finally block (if present) will execute. A program can contain multiple finally blocks for the same try. A finally block is written after the try (and optional catch) blocks, not inside the try body. A finally block is commonly used for cleanup such as closing files, network sockets, or database connections.

Difficulty: Easy

Correct Answer: 2 and 5 only

Explanation:


Introduction / Context:
This question checks core knowledge of C#.NET exception handling, especially the purpose and behavior of the finally block. Understanding which statements are universally true helps you write reliable cleanup logic and avoid misconceptions about abrupt termination.



Given Data / Assumptions:

  • C# uses try/catch/finally for exception handling.
  • finally is optional but, when present, is intended for cleanup.
  • We assume normal runtime conditions (no process-aborting failures such as Environment.FailFast, power loss, or thread aborts).


Concept / Approach:
In standard execution, finally runs regardless of whether an exception is thrown or caught. It is the recommended place for deterministic cleanup, such as disposing resources or closing handles. A single try block may be followed by zero or more catch blocks and at most one finally block.



Step-by-Step Solution:

Evaluate (1): False. Exceptions can be caught, allowing recovery. Abrupt termination happens only if the exception is unhandled or the process is explicitly terminated. Evaluate (2): True. finally executes whether or not an exception occurs (barring catastrophic termination). Evaluate (3): False. Per try, you can have many catch blocks but only one finally block. Evaluate (4): Wording is imprecise; finally is associated with try and written after try/catch blocks, not inside the try’s braces. The item is poorly phrased for a “correct” fact compared with (2) and (5). Evaluate (5): True. finally is used for cleanup (closing files/DB connections/sockets).


Verification / Alternative check:
Write sample code with try { … } catch { … } finally { Console.WriteLine("F"); }. You will observe the finally output in both success and failure cases.



Why Other Options Are Wrong:
Option A claims inevitable termination. Option D assumes multiple finally blocks per try. Option E ignores two clearly correct statements.



Common Pitfalls:
Believing finally will run even after process termination; misunderstanding that only one finally is allowed per try.



Final Answer:
2 and 5 only

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion