C# Exception StackTrace

Description

Exception StackTrace gets a string representation of the immediate frames on the call stack.

Syntax

Exception.StackTrace has the following syntax.


public virtual string StackTrace { get; }

Example

The following code example throws an Exception and then catches it and displays a stack trace using the StackTrace property.


//www . j  a v  a  2 s  .co m
using System;

class LogTableOverflowException : Exception
{
    const string overflowMessage = "The log table has overflowed.";

    public LogTableOverflowException( 
        string auxMessage, Exception inner ) :
            base( String.Format( "{0} - {1}", 
                overflowMessage, auxMessage ), inner )
    {
        this.HelpLink = "http://msdn.microsoft.com";
        this.Source = "Exception_Class_Samples";
    }
}
class OverflowDemo 
{
    public static void Main() 
    {
        try{
            throw new LogTableOverflowException( 
                String.Format( "Record \"{0}\" was not logged.", 
                    "asdf"), null );
        
        }catch( Exception ex ){
            Console.WriteLine( "\nStackTrace ---\n{0}", ex.StackTrace );
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version