Debugging

8
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Debugging K. R. C. Wijesinghe Trainer Virtusa Corporation

description

Debugging. K. R. C. Wijesinghe Trainer Virtusa Corporation. Debugging using Visual Studio .Net IDE. Compile Time Errors (Syntax Errors) E.g. for (int i = 0; i < V.Length; i++ ] { V [ i ] = 0; } Run-time Errors (Exceptions) E.g. for (int i = 0; i

Transcript of Debugging

Page 1: Debugging

Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Debugging

K. R. C. Wijesinghe

TrainerVirtusa Corporation

Page 2: Debugging

2Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Debugging using Visual Studio .Net IDE

Page 3: Debugging

3Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Types of Errors

• Compile Time Errors (Syntax Errors)E.g. for (int i = 0; i < V.Length; i++] {

V [ i ] = 0; }

• Run-time Errors (Exceptions)E.g. for (int i = 0; i <= V.Length; i++) {

V [ i ] = 0; }

• Logical ErrorsE.g. for (int i = 1; i < V.Length; i++) {

V [ i ] = 0; }

Page 4: Debugging

4Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Debug and Trace classes

• These classes are defined in System.Diagnosis namespace.

• Both classes has the same methods and properties.

• Methods in Debug class will work only if “DEBUG” macro is defined. (This is defined in Debug builds by default).

• Methods in Trace class will work only if “TRACE” macro is defined. (This is defined in Visual Studio.NET projects by default).

Page 5: Debugging

5Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

ASSERTS

• Asserts are used to ensure that a given condition is true at a given point in the program.

• Asserts will stop the program execution temporarily, if the condition is false, and provide you with three options. They are “Abort”, ”Retry” and “Ignore”.• Abort – Stop the program execution.• Retry – Take you to the Debug mode.• Ignore – Continue with the program.

E.g.

Stop execution if Obj is nullTrace.Assert (Obj != null);

Stop execution and display the message “Out of Range” if n > 100.Trace.Assert (n > 100, “Out of Range”);

Page 6: Debugging

6Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Logging

• Dumping information about intermediate steps of the program to an output device, without stopping the execution.

• This is very important in locating errors in complex programs.

• The default output is going to the “Output” window of Visual Studio.NET IDE.

• It can be send to console, a file or Windows event log.

Page 7: Debugging

7Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Logging Example

Trace.WriteLine (“Begin Section 1”);int n = 100;Trace.Indent( );Trace.WriteLine(“Inside Section 1”);Trace.WriteLine( n );n += 100;Trace.WriteLineIf (n > 150, “n greater than 150”);Trace.Unindent( );Trace.WriteLine(“End Section 1”);

Output:

Begin Section1Inside Section 1100 n greater than 150

End Section 1

Page 8: Debugging

8Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Sending output to Other devices

// Output to Console Trace. Listeners.Add (new TextStreamTraceListener(Console.Out);

// Output to a file TextWriterTraceListener TraceFile = new TextWriterTraceListener(@“C:\Temp\

Test.log”); Trace.Listeners.Add (TraceFile ); ... TraceFile.Close();

// Output to event log EventLogTraceListener ELog = new EventLogTraceListener("TestLog1"); Trace.Listeners.Add (ELog);

Trace.WriteLine( “Test Log Message”);