Click here to Skip to main content
13,410,463 members (36,839 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 21 Nov 2007

Navigating Exception Backtraces in Visual Studio

, 21 Nov 2007
Rate this:
Please Sign up or sign in to vote.
A code snippet to print exceptions and inner exceptions in Visual Studio's error parser format.


Ever been angry about Visual Studio's inability to provide easy exception backtrace navigation? Here is the solution.

I wrote a nice little debug helper function called PrintException which has proven very useful to me and my team for our everyday work. It prints the relevant information about an exception and its inner exceptions to the output recursively. The clue is, that it does this in a format that is understood by the error parser of Microsoft Visual Studio. So if you get an exception, you can easily navigate to the exact location in your source files by double-clicking on the lines in the printed stacktrace. It also prints all inner exceptions recursively with increasing indent for better readability.

Using the Code

Basically all you need to do is to wrap your main routine with a try-catch statement and call Debug.PrintException(e); on the caught exception e. Once the exception has been printed to standard out, you may also stop the debugger and you still can navigate to the source of error by double-clicking on a backtrace line. Here is an example:

public static class PrintExceptionTest
  static void test()
    try { test1(); }
    catch (Exception e) { throw new Exception("outer exception message", e); }

  static void test1()
    throw new InvalidOperationException("inner exception message");

  public static void Main()
    try { test(); }
    catch (Exception e) { Debug.PrintException(e); }

The formatted exceptions in the output window look like this:

Exception: "outer exception message"
   InvalidOperationException: "inner exception message"
     c:\C#\snippets\print_exception.cs(58,1):   PrintExceptionTest.test1()
     c:\C#\snippets\print_exception.cs(48,1):   PrintExceptionTest.test()
  c:\C#\snippets\print_exception.cs(52,1):   PrintExceptionTest.test()
  c:\C#\snippets\print_exception.cs(65,1):   PrintExceptionTest.Main()

As I have experienced, the innermost exception is the most important one, so I do a depth first recursion on the inner exceptions. Your mileage may vary.

Note: Error parser formatting works, of course, only when the exception is raised in an assembly that has been compiled in debug mode. Also the redirection of standard out to Visual Studio's output window (the only place where you can navigate using the built-in error parser) is only activated by default for a Windows Forms application, not for a Console application.

The Code

Since the code snippet that does the exception formatting is so tiny I put it up here directly:

using System;

public static class Debug
  public static void PrintException(Exception exception)
    PrintException(exception, "");

  public static void PrintException(Exception exception, string indent)
    string stars = new string('*', 80);
    Console.WriteLine(indent + stars);
    Console.WriteLine(indent + "{0}: \"{1}\"", 
	exception.GetType().Name, exception.Message);
    Console.WriteLine(indent + new string('-', 80));
    if (exception.InnerException != null)
      Console.WriteLine(indent + "InnerException:");
      PrintException(exception.InnerException, indent + "   ");
    foreach (string line in exception.StackTrace.Split(new string[] 
	{ " at " }, StringSplitOptions.RemoveEmptyEntries))
      if (string.IsNullOrEmpty(line.Trim())) continue;
      string[] parts;
      parts = line.Trim().Split(new string[] { " in " }, 
      string class_info = parts[0];
      if (parts.Length == 2)
        parts = parts[1].Trim().Split(new string[] { "line" }, 
        string src_file = parts[0];
        int line_nr = int.Parse(parts[1]);
        Console.WriteLine(indent + "  {0}({1},1):   {2}", 
		src_file.TrimEnd(':'), line_nr, class_info);
        Console.WriteLine(indent + "  " + class_info);
    Console.WriteLine(indent + stars);

That's it. Works very well for me. Let me know if you came across a special case where the formatting did not work.

If you are interested in my other articles, see my blog on C# and Ruby.


  • 22nd November, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

henon the hacker
Chief Technology Officer
Austria Austria
Henon tries to think pure and loves the programming languages C# and Ruby.

You may also be interested in...

Comments and Discussions

GeneralA class for getting the formatted exception text based on the code in this article Pin
jawwadalam25-Jun-09 6:28
memberjawwadalam25-Jun-09 6:28 
GeneralGood Idea Pin
mkavaleuski10-Dec-07 3:22
membermkavaleuski10-Dec-07 3:22 
QuestionCouldn't one do it like this ? Pin
xavier_k2y26-Nov-07 18:59
memberxavier_k2y26-Nov-07 18:59 
AnswerRe: Couldn't one do it like this ? Pin
henon the hacker27-Nov-07 0:46
memberhenon the hacker27-Nov-07 0:46 
GeneralRe: Couldn't one do it like this ? Pin
xavier_k2y27-Nov-07 1:26
memberxavier_k2y27-Nov-07 1:26 
GeneralCool Idea Pin
merlin98123-Nov-07 4:53
membermerlin98123-Nov-07 4:53 
GeneralNice Pin
Corinna John22-Nov-07 4:28
memberCorinna John22-Nov-07 4:28 
GeneralRe: Nice Pin
henon the hacker22-Nov-07 6:28
memberhenon the hacker22-Nov-07 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180221.1 | Last Updated 22 Nov 2007
Article Copyright 2007 by henon the hacker
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid