C#.NET — Exception handling rules: pick the true statements. try blocks cannot be nested. Only one try block is allowed per function. An exception must be caught in the same function where it is thrown. Values set in the exception object are available in the catch block. When throwing a user-defined exception, multiple data values can be stored in the exception object (for retrieval in catch).

Difficulty: Easy

Correct Answer: 4 and 5 only

Explanation:


Introduction / Context:
This item probes practical exception-handling rules in C#: nesting, propagation between methods, and how exception objects carry data that catch blocks can inspect.



Given Data / Assumptions:

  • Nesting try/catch is allowed.
  • Exceptions can propagate up the call stack.
  • Exception objects are real objects whose properties can be read in catch.
  • User-defined exceptions often include extra fields/properties.


Concept / Approach:
Statements (1)–(3) are common myths. C# supports nested try/catch/finally; you can use multiple try blocks in one method; and exceptions need not be caught where thrown—they may bubble up. Statements (4) and (5) reflect how exception objects are used in practice.



Step-by-Step Solution:

(1) False — nesting is legal and frequently used. (2) False — you may have several try blocks in one function. (3) False — exceptions can be caught in callers; only unhandled exceptions reach the runtime’s policy. (4) True — catch (ExType ex) lets you read ex.Message, ex.Data, custom properties, etc. (5) True — custom exceptions commonly add fields/auto-properties for multiple values (e.g., Expected, Actual).


Verification / Alternative check:
Create a custom exception with multiple properties; throw in one method, catch in another; read those properties inside catch successfully.



Why Other Options Are Wrong:
Options A/B/C assert false constraints; option E marks all true when only (4) and (5) are true.



Common Pitfalls:
Believing throws must be caught locally; forgetting you can add rich context to exceptions.



Final Answer:
4 and 5 only

Discussion & Comments

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