Export (0) Print
Expand All

Exception.Exception() Constructor

Initializes a new instance of the Exception class.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public Exception ()
public Exception ()
public function Exception ()
Not applicable.

This constructor initializes the Message property of the new instance to a system-supplied message that describes the error and takes into account the current system culture.

All the derived classes should provide this default constructor. The following table shows the initial property values for an instance of Exception.

Property

Value

InnerException

A null reference (Nothing in Visual Basic).

Message

A system-supplied localized description.

The following code example derives an Exception that uses a predefined message. The code demonstrates the use of the parameterless constructor for the derived class and the base Exception class.

// Example for the Exception( ) constructor.
using System;

namespace NDP_UE_CS
{
    // Derive an exception with a predefined message.
    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() 
        {
            Console.WriteLine( 
                "This example of the Exception( ) constructor " +
                "generates the following output." );
            Console.WriteLine( 
                "\nHere, an exception is thrown using the \n" +
                "parameterless constructor of the base class.\n" );

            CalcHalf( 12 );
            CalcHalf( 15 );

            Console.WriteLine( 
                "\nHere, an exception is thrown using the \n" +
                "parameterless constructor of a derived class.\n" );

            CalcHalf2( 24 );
            CalcHalf2( 27 );
        }
    	
        // Half throws a base exception if the input is not even.
        static int Half( int input )
        {
            if( input % 2 != 0 )
                throw new Exception( );

            else return input / 2;
        }

        // Half2 throws a derived exception if the input is not even.
        static int Half2( int input )
        {
            if( input % 2 != 0 )
                throw new NotEvenException( );

            else return input / 2;
        }

        // CalcHalf calls Half and catches any thrown exceptions.
        static void CalcHalf(int input )
        {
            try
            {
                int halfInput = Half( input );
                Console.WriteLine( 
                    "Half of {0} is {1}.", input, halfInput );
            }
            catch( Exception ex )
            {
                Console.WriteLine( ex.ToString( ) );
            }
        }

        // CalcHalf2 calls Half2 and catches any thrown exceptions.
        static void CalcHalf2(int input )
        {
            try
            {
                int halfInput = Half2( input );
                Console.WriteLine( 
                    "Half of {0} is {1}.", input, halfInput );
            }
            catch( Exception ex )
            {
                Console.WriteLine( ex.ToString( ) );
            }
        }
    }
}

/*
This example of the Exception( ) constructor generates the following output.

Here, an exception is thrown using the
parameterless constructor of the base class.

Half of 12 is 6.
System.Exception: Exception of type System.Exception was thrown.
   at NDP_UE_CS.NewExceptionDemo.Half(Int32 input)
   at NDP_UE_CS.NewExceptionDemo.CalcHalf(Int32 input)

Here, an exception is thrown using the
parameterless constructor of a derived class.

Half of 24 is 12.
NDP_UE_CS.NotEvenException: The argument to a function requiring even input is
not divisible by 2.
   at NDP_UE_CS.NewExceptionDemo.Half2(Int32 input)
   at NDP_UE_CS.NewExceptionDemo.CalcHalf2(Int32 input)
*/

// Example for the Exception() constructor.
package NDP_UE_JSL ; 

import System.* ;

// Derive an exception with a predefined message.
class NotEvenException extends System.Exception
{
    public NotEvenException()
    {
        super("The argument to a function requiring " 
            + "even input is not divisible by 2.");
    } //NotEvenException
} //NotEvenException

class NewExceptionDemo
{
    public static void main(String[] args)
    {
        Console.WriteLine(("This example of the Exception( ) constructor " 
            + "generates the following output."));
        Console.WriteLine(("\nHere, an exception is thrown using the \n" 
            + "parameterless constructor of the base class.\n"));
        CalcHalf(12);
        CalcHalf(15);
        Console.WriteLine(("\nHere, an exception is thrown using the \n" 
            + "parameterless constructor of a derived class.\n"));
        CalcHalf2(24);
        CalcHalf2(27);
    } //main

    // Half throws a base exception if the input is not even.
    static int Half(int input) throws System.Exception
    {
        if (input % 2 != 0) {
            throw new System.Exception();
        }
        else {
            return input / 2;
        }
    } //Half

    // Half2 throws a derived exception if the input is not even.
    static int Half2(int input) throws NotEvenException
    {
        if (input % 2 != 0) {
            throw new NotEvenException();
        }
        else {
            return input / 2;
        }
    } //Half2

    // CalcHalf calls Half and catches any thrown exceptions.
    static void CalcHalf(int input)
    {
        try {
            int halfInput = Half(input);

            Console.WriteLine("Half of {0} is {1}.", 
                System.Convert.ToString(input), 
                System.Convert.ToString(halfInput));
        }
        catch (System.Exception ex) {
            Console.WriteLine(ex.toString());
        }
    } //CalcHalf

    // CalcHalf2 calls Half2 and catches any thrown exceptions.
    static void CalcHalf2(int input)
    {
        try {
            int halfInput = Half2(input);

            Console.WriteLine("Half of {0} is {1}.", 
                System.Convert.ToString(input), 
                System.Convert.ToString(halfInput));
        }
        catch (System.Exception ex) {
            Console.WriteLine(ex.toString());
        }
    } //CalcHalf2
} //NewExceptionDemo
   
/*
This example of the Exception( ) constructor generates the following output.

Here, an exception is thrown using the
parameterless constructor of the base class.

Half of 12 is 6.
System.Exception: Exception of type 'System.Exception' was thrown.
   at NDP_UE_JSL.NewExceptionDemo.Half(Int32 input) in C:\Documents and Settings
\My Documents\Visual Studio\Projects\ConsoleApp - JS\ConsoleApp
 - JS\Class1.jsl:line 33
   at NDP_UE_JSL.NewExceptionDemo.CalcHalf(Int32 input) in C:\Documents and Sett
ings\My Documents\Visual Studio\Projects\ConsoleApp - JS\Consol
eApp - JS\Class1.jsl:line 59

Here, an exception is thrown using the
parameterless constructor of a derived class.

Half of 24 is 12.
NDP_UE_JSL.NotEvenException: The argument to a function requiring even input is
not divisible by 2.
   at NDP_UE_JSL.NewExceptionDemo.Half2(Int32 input) in C:\Documents and Setting
s\My Documents\Visual Studio\Projects\ConsoleApp - JS\ConsoleAp
p - JS\Class1.jsl:line 46
   at NDP_UE_JSL.NewExceptionDemo.CalcHalf2(Int32 input) in C:\Documents and Set
tings\My Documents\Visual Studio\Projects\ConsoleApp - JS\Conso
leApp - JS\Class1.jsl:line 74

*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2015 Microsoft