Searching for "IOException"

Q:

You responsible for maintaining an application that was written by a former colleague at XYZ.

 

The application reads from and writes to log files located on the local network. The original author included the following debugging code to facilitate maintenance:

try {
Debug.WriteLine(“Inside Try”);
throw(new IOException());}
catch (IOException e) {
Debug.WriteLine (“IOException Caught”);}
catch (Exception e) {
Debug.WriteLine(“Exception Caught”);}.
finally {
Debug.WriteLine (“Inside Finally”);}
Debug.WriteLine (“After End Try”);

 

Which output is produced by thus code?

A) Inside Try Exception Caught IOException Caught Inside Finally After End Try B) Inside Try Exception Caught Inside Finally After End Try
C) Inside Try IOException Caught Inside Finally After End Try D) Inside Try IOException Caught Inside Finally
 
Answer & Explanation Answer: C) Inside Try IOException Caught Inside Finally After End Try

Explanation:

First the try code runs. Then one single exception, the IOException occurs, not two exceptions.Then the Finally code segments executes. After Finally code bas been executed normal application resumes at the next line after the line that called the error. In this case, the After End Try code runs.

Report Error

View Answer Report Error Discuss

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?

A) try { XYZ(); LogError(); } catch (Exception e) { CleanUp(e); } B) try { XYZ(); } catch (Exception e) { LogError(e); CleanUp(); }
C) try { XYZ(); } catch (Exception e) { LogError(e); } finally { CleanUp(); } D) try { XYZ(); } catch (Exception e) { CleanUp(e); } finally { LogError(); }
 
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.

Report Error

View Answer Report Error Discuss