Q:
You develop a Windows-based application that enables to enter product sales. You add a subroutine named XYZ.
You discover that XYZ sometimes raises an IOException during execution. To address this problem you create two additional subroutines named LogError and CleanUp. These subroutines are governed by the following rules:
• LogError must be called only when XYZ raises an exception.
• CleanUp must be called whenever XYZ is complete.
You must ensure that your application adheres to these rules. Which code segment should you use?
Answer & Explanation
Answer: C) try { XYZ(); } catch (Exception e) { LogError(e); } finally { CleanUp(); }
Explanation: We must use a try…catch…finally construct. First we run the Comapany() code in the try block.Then we use the LogError() subroutine in the catch statement since all exceptions are handled here. Lastly we put the CleanUp() subroutine in the finally statement since this code will be executed regardless of whether an exception is thrown or not.
View Answer
Report Error
Discuss