80 likes | 192 Views
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 <= V.Length; i++)
E N D
Debugging K. R. C. Wijesinghe Trainer Virtusa Corporation
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 Errors E.g. for (int i = 1; i < V.Length; i++) { V [ i ] = 0; } Types of Errors
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). Debug and Trace classes
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 null Trace.Assert (Obj != null); Stop execution and display the message “Out of Range” if n > 100. Trace.Assert (n > 100, “Out of Range”); ASSERTS
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. Logging
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 Section1 Inside Section 1 100 n greater than 150 End Section 1 Logging Example
// 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”); Sending output to Other devices