Monday, May 21, 2007

Debugging and Error Handling

Different Types are Errors are:
· Fatal Errors à Errors that cause applications to fail completely
· Syntax Errors à Simple Errors in code that prevent compilation
· Semantic Errors (also known as Logic Errors) à Application logic is in some way flawed.

Debugging Methods available in VS
Debug builds maintain symbolic information about your application, so that VS is capable of knowing exactly what is happening as each line of code is executed. The symbolic information is contained in .pdb files, which you may have seen appearing in Debug directories on your computer. This enables you to perform many useful operations, which include:
· Outputting debugging information to VS
· Looking at the values of variables in scope during application execution
· Pausing and restarting program execution
· Automatically halting execution at certain points in the code
· Stepping through program execution a line at a time
· Monitoring changes in variable content during application execution
· Modifying variable content at runtime
· Performing test calls of functions

In the Output Window, you can see information relating to the compilation and execution of code, including errors encountered during compilation and so forth. This window has two modes: Build mode and Debug mode. These modes show you compilation and runtime information, respectively.

Debugging in Nonbreak (Normal) Mode
How do you output information to the Output Window at runtime?
Writing text to the Output window at runtime is very simple. You simply need to replace calls to Console.WriteLine() with the required call to write text where you want it. There are two commands you can use to do this:
· Debug.WriteLine() à this command works only for debug build. This won’t even be compiled into a release build. It will just disappear.
· Trace.WriteLine() à this command works for both the debug and release builds
Two versions of your application can be created from a single source file. The debug version displays all kinds of extra diagnostic information, whereas the release version won’t have this overhead.

Syntax for the general output of these functions is as follows:
:
This allows you to see at a glance what output messages are displayed in the Output window, which is useful for times when similar messages are output from different places in the application.

For Example:
Debug.WriteLine(“Added 1 to I”, “MyFunc”);
Result is:
MyFunc: Added 1 to i

Where does the Debug.WriteLine() and Trace.WriteLine() commands are present?
In the System.Diagnostics namespace.

What are the other commands that are equivalent to Console.Write() command?
· Debug.Write()
· Trace.Write()

And the other different Commands are as follows and they take the Boolean value parameters:
· Debug.WriteLineIf()
· Trace.WriteLineIf()
· Debug.WriteIf()
· Trace.WriteIf()

What command is used as an alternative to writing information to the Output window?
An alternative to writing information to the Output window is to use trace points. This is a feature of VS rather than C#, but serve the same function as using Debug.WriteLine(). They output debugging information without modifying your code.

What are the steps to add a trace point to the code?


Error-Handling techniques available in C#

No comments: