DRAG DROP
You develop an application that displays information from log files when errors occur. The application will
prompt the user to create an error report that sends details about the error and the session to the administrator.
When a user opens a log file by using the application, the application throws an exception and closes.
The application must preserve the original stack trace information when an exception occurs during this
process.
You need to implement the method that reads the log files.
You have the following code:
Which code segments should you include in Target 1 and Target 2 to complete the code? (To answer, drag the
appropriate code segments to the correct locations in the answer area. Each code segment may be used once,
more than once, or not at all. You may need to drag the split bar between panes or scroll to view content.)
Select and Place:
Explanation:
StringReader – Implements a TextReader that reads from a string.
http://msdn.microsoft.com/en-us/library/system.io.stringreader(v=vs.110).aspx
StreamReader – Implements a TextReader that reads characters from a byte stream in a particular encoding.
http://msdn.microsoft.com/en-us/library/system.io.streamreader(v=vs.110).aspx
Once an exception is thrown, part of the information it carries is the stack trace. The stack trace is a list of the
method call hierarchy that starts with the method that throws the exception and ends with the method that
catches the exception. If an exception is re-thrown by specifying the exception in the throw statement, the stack
trace is restarted at the current method and the list of method calls between the original method that threw the
exception and the current method is lost. To keep the original stack trace information with the exception, use
the throw statement without specifying the exception.
http://msdn.microsoft.com/en-us/library/ms182363(v=vs.110).aspx
StreamReader
throw
StreamReader needs an Encoding instance that specificies which encoding should be used. This piece of code is not in the question
So we need StringReader.
throw will throw an exception with original exception as InnerException, preserving the stackTrace.
wrong. constructor of StringReader accepts string, not path to file.
StreamReader is correct answer
Answer is : StreamReader and Throw … Confirmed .. StringReader code will read only the file name while StreamReader will read the content of files which is required
using (StringReader sr = new StringReader(“D:\\hello.txt”))
{
try
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(“String Reader : ” + line);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
}
}
using (StreamReader sr = new StreamReader(“D:\\hello.txt”))
{
try
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(“Stream Reader : ” + line);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
}
}
Console.ReadLine();