You are maintaining a Windows application that calls a subroutine named XYZ, which sometimes throws an IOException. You have also created two subroutines named LogError and CleanUp. LogError must be called only when XYZ raises an exception, and CleanUp must always be called whenever XYZ finishes, whether it fails or succeeds. Which try/catch/finally structure correctly implements these requirements?

Difficulty: Easy

Correct Answer: try { XYZ(); } catch (Exception e) { LogError(e); } finally { CleanUp(); }

Explanation:


Introduction / Context:
Exception handling in .NET relies heavily on the try, catch and finally blocks. It is common to need one block of code that runs only when an error occurs, and another block that must always run regardless of success or exception. This question tests your understanding of where to place logging and cleanup logic so that LogError is called only when an exception is thrown and CleanUp is always executed at the end of the operation.



Given Data / Assumptions:

  • XYZ is a subroutine that can sometimes throw an IOException or other exceptions.
  • LogError(e) is a method that should execute only when an exception occurs.
  • CleanUp() is a method that must always run when XYZ is finished, both in success and failure scenarios.
  • You are writing C# style pseudocode using try, catch and finally blocks.



Concept / Approach:
In C#, any code placed inside a catch block executes only when an exception of a matching type is thrown from within the corresponding try block. Code placed inside a finally block executes regardless of whether an exception was thrown or not, and it runs even if the exception is not caught locally, as long as the process is still unwinding the stack. Therefore, error logging logic should go into the catch block, and cleanup logic should go into the finally block so that resources are always released or finalized.



Step-by-Step Solution:
1. Wrap the XYZ() call inside a try block: try { XYZ(); }. 2. Add a catch block that catches Exception (or IOException if you want to narrow it) and calls LogError(e) to record the error details. 3. Add a finally block after the catch, and place CleanUp() inside this block. 4. With this structure, if XYZ() executes successfully, the catch block is skipped and only CleanUp() runs in finally. 5. If XYZ() throws an exception, LogError(e) runs in the catch block, and then CleanUp() runs in the finally block, ensuring cleanup always occurs.



Verification / Alternative check:
You can verify the behavior by writing a small test where XYZ sometimes throws an exception and sometimes completes successfully. By adding diagnostic messages inside LogError and CleanUp, you can see that LogError only appears when there is an exception, while CleanUp appears in both the success and failure runs. This confirms that the structure meets the requirement of conditional logging and unconditional cleanup.



Why Other Options Are Wrong:
Option A is wrong because LogError() is inside the try block, so it would execute even if XYZ() does not throw an exception, violating the requirement. Option B is wrong because CleanUp() is inside the catch block and will only run when there is an exception, not after a successful call. Option D is wrong because it calls CleanUp(e) (which does not match the CleanUp signature) and places LogError() in the finally block, meaning LogError would run unconditionally rather than only on error.



Common Pitfalls:
A common mistake is putting cleanup code inside the catch block, which prevents it from running after successful execution. Another pitfall is catching exceptions but forgetting to log or rethrow them, which hides failures. Developers may also place logging in finally, causing log entries that do not correspond to real errors. Correct use of try/catch/finally helps create robust applications that both record failures accurately and always release resources properly.



Final Answer:
The correct structure is to call XYZ() inside try, call LogError(e) inside catch when an exception occurs, and call CleanUp() inside finally so that cleanup always executes: try { XYZ(); } catch (Exception e) { LogError(e); } finally { CleanUp(); }.

More Questions from Microsoft Certification

Discussion & Comments

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