Visual Studio
stack trace
debugging
error handling
software development

How to view the call stack (stack trace) in Visual Studio

Codemia Knowledge Hub ·

Start a debug session (F5), and when execution stops at a breakpoint or exception, open Debug > Windows > Call Stack. That window shows the sequence of method calls that led to the current point. When an exception is thrown, the Exception Helper also shows the stack trace directly.

What a stack trace shows

A stack trace is a report of the active stack frames at a point in time, usually when an exception occurs. It shows the sequence of method calls that led to the error, and each entry typically includes:

  • The method name
  • The file name
  • The line number

Viewing the stack at a breakpoint

When a breakpoint is hit, open Debug > Windows > Call Stack (Ctrl+Alt+C) to see the calls that led to the current point. The window updates as you step through code.

Viewing the stack when an exception occurs

The Exception Helper pops up with the exception type and message. Its View Details link opens the exception object, whose StackTrace property holds the trace with file and line for each call, and the Call Stack window shows the same frames.

Using the Call Stack window

  • Opening it:
    • Go to Debug > Windows > Call Stack.
    • You may need to start a debug session first by running the app in debug mode (F5).
  • Features:
    • Double-click to navigate: double-click a line to jump to that line in your code.
    • Right-click options: set the current execution point or copy the entire stack trace.

Controlling what the stack trace includes

  • Just My Code: shows only your project's code, hiding system and external library calls. Enable it via Debug > Options > General > "Enable Just My Code".
  • Symbols: frames show file names and line numbers only when the module's .pdb symbols load. Add symbol locations under Debug > Options > Debugging > Symbols.
  • Source files: if the debugger cannot find a source file, add its folder under the solution's Properties > Common Properties > Debug Source Files.

Example

Consider a code snippet that leads to a division by zero:

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        Console.WriteLine(Divide(10, 0));
8    }
9
10    static int Divide(int a, int b)
11    {
12        return a / b; // DivideByZeroException thrown here
13    }
14}

Run it with F5. The debugger breaks on the return a / b; line, the Exception Helper reports System.DivideByZeroException, and the Call Stack window lists Program.Divide at the top with Program.Main below it. Double-click the Main frame to see the call that passed 0.


Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms