Resources

Testing and Debugging Your C# Application

Lesson 1

Error Types

Syntax Errors

Run Time

Logical

Break Mode

Breakpoints

Debugging Windows

Lesson 2

Debug and Trace

Methods

Listeners Collection

// Open (or create) file
System.IO.FileStream myLog = new System.IO.FileStream("C:\\myfile.txt", System.IO.FileMode.OpenOrCreate);

// Create TraceListener logging to specified file
TextWriterTraceListener myListener = new TextWriteTraceListener(myLog);

// Add to listeners collection
Trace.Listeners.Add(myListener);
// Create event log entitled "DebugLog"
EventLog myLog = new EventLog("Debug Log");

// Set EventLog source property (avoid error)
myLog.source = "Trace Output";

// Create EventLogTraceListener
EventLogTraceListener = new EventLogTraceListener(myLog);

Trace Switches

BooleanSwitch myBoolSwitch = newBooleanSwitch("Switch1", "Control Data Tracing");

TraceSwitch myTraceSwitch = new TraceSwitch("Switch2", "Control Form Tracing");
Trace.WriteIf(myBoolSwitch.Enabled == true, "error");

Trace.WriteIf(myTraceSwitch.TraceInfo == true, "type mismatch");

Configure Trace Switches

<?xml version="1.0" encoding="Windows-1252"?>
<configuration>
< system.diagnostics>
<switches>
<add name="myBoolSwitch" value="0"/>
<add name="myTraceSwitch" value="3"/>
</switches>
< /system.diagnostics>
</configuration>

Lesson 3

Unit Test Plan

Test Case Design

Lesson 4

Exceptions

Exception Handler

public void Parse(string a string)
{
try
{
double aDouble;
aDouble = Double.Parse(aString);
}

catch (System.ArgumentNullException e)
{
// Code to handle null argument - e
// contains ref to exception (access to
// its info)
}

catch
{
// Catch any other exception
}

finally
{
// Code that must be executed
}
}

Throwing Exceptions

try
{

}

catch (System.NullReferenceException e)
{
// Assuming exception can not be handled
// rethrow

throw e;
}
throw new NullReferenceException("Widget A is not set", e);
public class WidgetException:System.ApplicationException
{
// Var to hold widget

Widget mWidget;

public Widget ErrorWidget
{
get
{
return mWidget;
}
}

// Constructor takes widget and string

//describing error conditions
public WidgetException(Widget W, string S) : base(S)
{
mWidget = W;
}
}

Widget Alpha;

throw new WidgetException(Alpha,"Alpha is corrupt");

Downloads