C# Exception Source

Description

Exception Source gets or sets the name of the application or the object that causes the error.

Syntax

Exception.Source has the following syntax.


public virtual string Source { get; set; }

Example

The following code example throws an Exception that sets the Source property in its constructor and then catches the exception and displays Source.


// www . j  ava 2  s.  c o 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( "\nSource ---\n{0}", ex.Source );

        }
    }
}

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