C# Exception Exception()

Description

Exception Exception() initializes a new instance of the Exception class.

Syntax

Exception.Exception() has the following syntax.


public Exception()

Example

The following code example derives an Exception that uses a predefined message.


using System;//from  ww w  . j  a va2s .co m

class NotEvenException : Exception
{
    public NotEvenException( ) :
        base( "The argument to a function requiring " +
            "even input is not divisible by 2." )
    { }
}

class NewExceptionDemo 
{
    public static void Main() 
    {
        try{
            throw new Exception( );
            throw new NotEvenException( );
        }
        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