SkillPilotAI

Making friends with debugging

Programs fail in two ways: they crash with an exception, or they silently give a wrong answer. Reading a stack trace and using breakpoints, stepping and Watch are daily developer skills, and "how do you debug?" is a common interview question.

3.1 Why is it needed?

  • A crash gives you a message and a stack trace: you must know how to read it.
  • A silent bug gives no message at all: the answer is just wrong.
  • For silent bugs you need to pause the program and see the real values: that is what a debugger does.
  • Guessing wastes time; real values point straight at the root cause.

3.2 Crash: reading the exception and stack trace

C#
Console.Write("Bill amount: ");int total = int.Parse(Console.ReadLine()!);Console.Write("Kitne log: ");int people = int.Parse(Console.ReadLine()!); PrintShare(total, people); void PrintShare(int amount, int count){    int share = amount / count;    Console.WriteLine($"Har ek ko: {share}");}
Output
$ dotnet run bill.csBill amount: 1200Kitne log: 0Unhandled exception. System.DivideByZeroException: Attempted to divide by zero.   at Program.<<Main>$>g__PrintShare|0_0(Int32 amount, Int32 count) in ...\bill.cs:line 10   at Program.<Main>$(String[] args) in ...\bill.cs:line 6
  • Exception type (DivideByZeroException): what happened.
  • Message (Attempted to divide by zero.): the reason in plain English.
  • Stack trace, read top to bottom: the top "at" line is where it was thrown (bill.cs line 10).
  • The next "at" line is the caller (PrintShare called from line 6).
  • Strange names like <<Main>$>g__PrintShare|0_0 are compiler-generated: just look for your own method name.

3.3 Silent bug: the discount never applied

C#
decimal price = 2499;int percent = 15; decimal final = ApplyDiscount(price, percent);Console.WriteLine($"Final price: {final}"); decimal ApplyDiscount(decimal amount,    int percent){    decimal off = amount * (percent / 100);    return amount - off;}
Output
$ dotnet run discount.csFinal price: 2499
  • 15% off a 2499 Mixer Grinder should not still cost 2499, yet there is no error.
  • No stack trace here, so we use the debugger.

3.4 Breakpoint, step over, step into

KeyActionWhat it does
F9BreakpointRed dot on a line; execution pauses there before running it
F5Start debuggingRuns until the next breakpoint
F10Step overRuns the whole current line (including calls) and stops at the next line
F11Step intoGoes inside the method called on the current line
Locals / WatchWindowsLocals: current variable values; Watch: any expression you type
Output
Breakpoint hit: discount.cs, line 4Locals  price    2499  percent  15  final    0 F10 Step Over: line 4 doneLocals  final  2499 F11 Step Into: ApplyDiscount, line 10Locals  amount   2499  percent  15  off      0 F10 Step Over: line 10 doneLocals  amount   2499  percent  15  off      0
  • F10 on line 4 only shows the same wrong result; it hides what happens inside ApplyDiscount.
  • Restart, press F11 to enter ApplyDiscount, then F10 over line 10: off is 0.

3.5 Watch: finding the root cause

Output
Watch  percent / 100     0  off               0  percent / 100m    0.15
  • percent / 100 is 0: int divided by int drops the decimal part (15 / 100 = 0).
  • With a decimal literal (100m) it is 0.15.
  • Fix: multiply first, then divide. Since amount is decimal, the whole expression becomes decimal.
C#
decimal ApplyDiscount(decimal amount,    int percent){    decimal off = amount * percent / 100;    return amount - off;}
Output
$ dotnet run discount.csFinal price: 2124.15
Debugging in 4 steps
  1. 1.Reproduce: give the same input that causes the bug

  2. 2.Exception? Read the stack trace. Silent bug? Breakpoint on the suspicious line

  3. 3.Step into (F11) / over (F10) and check Locals + Watch

  4. 4.Find the root cause from real values (percent / 100 = 0)

  5. 5.Fix and re-run with the same input

Do not guess, fix from real values

3.6 Hot Reload: dotnet watch

  • Start a normal console project with dotnet watch instead of dotnet run.
  • Edit code and save: the change is applied without restarting the app.
  • Not everywhere: file-based apps restart by default, and bigger changes need a restart (Ctrl+R).
C#
while (true){    Console.WriteLine(PriceLabel(2499));    await Task.Delay(2000);} string PriceLabel(decimal price)    => $"Price: {price}";
Output
$ dotnet watchdotnet watch Hot reload enabled.Price: 2499dotnet watch Waiting for changesPrice: 2499dotnet watch File updated: .\Program.csPrice: 2499dotnet watch C# and Razor changes applied in 5058ms.Price: Rs 2499 (GST incl.)Price: Rs 2499 (GST incl.)

3.7 Checkpoint: which line do you open?

Output
$ dotnet run bill.csBill amount: 1200Kitne log: abcUnhandled exception.System.FormatException: The input string  'abc' was not in a correct format.   at System.Number.ThrowFormatException...   at System.Int32.Parse(String s)   at Program.<Main>$(String[] args)        in ...\bill.cs:line 4
  • Open bill.cs line 4: the two lines above it are .NET's own code, not your bug.
  • Rule: go top to bottom and stop at the first line from your own file.
  • FormatException: int.Parse did not get a number.

3.8 Common mistakes

  • Patching the symptom: turning count 0 into 1 inside PrintShare stops the crash but prints "Har ek ko: 1200" (wrong).
  • Right fix: stop the bad value where it enters (validate input, show "Kam se kam 1 log chahiye").
  • Ignoring the error text instead of reading type + message + first own line.
  • Using F10 when the bug is inside a method (use F11).
C#
int people = int.Parse(Console.ReadLine()!); if (people <= 0){    Console.WriteLine("Kam se kam 1 log chahiye");    return;} PrintShare(total, people);

3.9 Interview tips

  • "How do you debug?": reproduce first, then read the stack trace from the top and find the first line of your own code.
  • For silent bugs: breakpoint, step through, check values in Locals and Watch.
  • Fix the root cause, not the symptom, and verify with the same input.
  • Know F9 / F5 / F10 / F11 and that dotnet watch gives Hot Reload.