PROG8060 Lesson02 Debugging
PROG8060 Lesson02 Debugging
PROG8060 Lesson02 Debugging
Can also hit the pause button when the application is running and
then see the values of the variables, etc…
Click to the <left> of the line number where you want to add a
Breakpoint:
Program here does not give any error…however, it doesn’t show
the correct result either
Run the program:
Hovering over any variables will give us the value of it
Next, press the F11 key to “Step Into” each line of code after the
breakpoint.
To remove the Breakpoint, simply click on the red dot that is
beside the line number:
Other information that could be valuable when debugging while
using a BreakPoint:
Call Stack Window: will show which part of the code is calling a
particular method as well as the sequence of access (for example, if you
have multiple parts of the code calling the same method, they each
could be changing values that you were not expecting);
}
static string titleProduct = "The product of the numbers 33 and 44 is:";
static void CalculateProduct()
{
int thirdNumber = 33;
int fourthNumber = 44;
int product = thirdNumber * fourthNumber;
Console.WriteLine(product);
}
static void Main()
{
Console.WriteLine(titleAdd);
CalculateSum();
Console.WriteLine(titleProduct);
CalculateProduct();
Console.ReadLine();
}
}
To avoid unexpected program crashes and to generate meaningful
responses for errors, we can use try-catch blocks. When an error is
encountered, the code in the try section is skipped and execution resumes in
the catch section.
try
{
using (var myObject = new MyClass())
{
// Actual code is here
}
}
catch(Exception ex)
{
// How to handle an exception here
}
When working with large programs (say, hundreds or thousands
of lines of code), it may be difficult to find exactly where the
error occurred
Sequence / Action
Selection / Decision
Iteration / Loop
The key building blocks of Algorithms:
Sequence / Action
▪ For example, sum var1 and var2
Selection / Decision
▪ If the temperature is greater than 27C, then print “it is hot outside”
Iteration / Loop
▪ Calculate the sum of all even numbers from zero to one-hundred
Print the name of your best friend