Exception.StackTrace and the .NET Compact Framework

Exception.StackTrace and the .NET Compact Framework

  • Comments 2

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)
    {
        GetStackTrace();
    }

    static void GetStackTrace()
    {
        // catch exception
        try
        {
            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.

Enjoy!
-- DK

Disclaimer(s):
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.

Page 1 of 1 (2 items)