C# Exception Message

Description

Exception Message gets a message that describes the current exception.

Syntax

Exception.Message has the following syntax.


public virtual string Message { get; }

Example

The following code example throws and then catches an ExceptionException and displays the exception's text message using the Message property.


/*w  w  w . ja  va  2s.co  m*/
using System;

class LogTableOverflowException : Exception
{
    const string overflowMessage = "overflowed.";

    public LogTableOverflowException(
        string auxMessage, Exception inner) :
        base(String.Format("{0} - {1}",
               overflowMessage, auxMessage), inner)
    {
        this.HelpLink = "http://java2s.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("\nMessage ---\n{0}", ex.Message);
        }
    }
}

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