C# Exception Exception(String, Exception)

Description

Exception Exception(String, Exception) initializes a new instance of the Exception class with a specified error message and a reference to the inner exception that is the cause of this exception.

Syntax

Exception.Exception(String, Exception) has the following syntax.


public Exception(
  string message,
  Exception innerException
)

Parameters

Exception.Exception(String, Exception) has the following parameters.

  • message - The error message that explains the reason for the exception.
  • innerException - The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.

Example

The following code example derives an Exception for a specific condition.


using System;//from   w  w w . j av  a  2s. c  o  m

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

    public LogTableOverflowException() :
        base(overflowMessage)
    { }

    public LogTableOverflowException(string auxMessage) :
        base(String.Format("{0} - {1}",
          overflowMessage, auxMessage))
    { }

    public LogTableOverflowException(
        string auxMessage, Exception inner) :
        base(String.Format("{0} - {1}",
               overflowMessage, auxMessage), inner)
    { }
}


class OverflowDemo
{
    public static void Main()
    {
        try
        {
            throw new LogTableOverflowException(
                String.Format("Record \"{0}\" was not logged.",
                    ""), new Exception("test"));

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

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