Exception.StackTrace and the .NET Compact Framework

One of the great new additions to version 2 of the .NET Compact Framework is the Exception.StackTrace property.  The StackTrace property provides the call stack at the time of the exception as a String.  The code below displays the contents of the Exception.StackTrace property in a MessageBox.  Additionally, this code generates an unhandled exception, in the same method.

Note: The code and output shown in this post was created using the Beta 2 releases of Visual Studio 2005 and the .NET Compact Framework version 2.

using System;
using System.Windows.Forms;

class Program
    static void Main(string[] args)

    static void GetStackTrace()
        // catch exception
            throw new ApplicationException();
        catch (Exception ex)
            MessageBox.Show(ex.StackTrace, “Stack Trace”);

        // do not catch exception
        //  this will display the unhandled exception dialog
        throw new ApplicationException();

The above code displays the following stack trace.

at Program.GetStackTrace()
at Program.Main()

If you compile the example code and run it (not under the debugger), you will first see a message box containing the above stack.  Clicking “Ok” in the message box will cause the unhandled exception dialog to be displayed.  If you click on the Details button, you will see the exception type (ApplicationException) and a stack trace.  Since both exceptions were encountered in the same method, you receive the same stack trace.

— DK

This posting is provided “AS IS” with no warranties, and confers no rights.
Some of the information contained within this post may be in relation to beta software.  Any and all details are subject to change.

Comments (2)

  1. Daniel Moth says:

    Some would ask how could you ship CF 1.0 without this feature 😉