次の方法で共有


Exception コンストラクタ ()

Exception クラスの新しいインスタンスを初期化します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)

構文

'宣言
Public Sub New
'使用
Dim instance As New Exception
public Exception ()
public:
Exception ()
public Exception ()
public function Exception ()

解説

このコンストラクタは、新しいインスタンスの Message プロパティを初期化して、その値として、現在のシステムのカルチャを反映した、エラーを説明するシステム提供のメッセージを指定します。

すべての派生クラスにこの既定のコンストラクタを用意することをお勧めします。Exception のインスタンスの初期プロパティ値を次の表に示します。

プロパティ

InnerException

null 参照 (Visual Basic の場合は Nothing)。

Message

システム提供のローカライズされた説明。

使用例

定義済みのメッセージを使用する Exception を派生させるコード例を次に示します。この例では、基本 Exception クラスと派生クラスのパラメータを受け取らないコンストラクタの使用方法を示します。

' Example for the Exception( ) constructor.
Imports System
Imports Microsoft.VisualBasic

Namespace NDP_UE_VB

    ' Derive an exception with a predefined message.
    Class NotEvenException
        Inherits Exception
           
        Public Sub New( )
            MyBase.New( _
                "The argument to a function requiring " & _
                "even input is not divisible by 2." )
        End Sub ' New
    End Class ' NotEvenException

    Module NewExceptionDemo
       
        Sub Main( )
            Console.WriteLine( _
                "This example of the Exception( ) constructor " & _
                "generates the following output." )
            Console.WriteLine( vbCrLf & _
                "Here, an exception is thrown using the " & vbCrLf & _
                "parameterless constructor of the base class." & _
                vbCrLf )

            CalcHalf( 12 )
            CalcHalf( 15 )
              
            Console.WriteLine(vbCrLf & _
                "Here, an exception is thrown using the " & vbCrLf & _
                "parameterless constructor of a derived class." & _
                vbCrLf )

            CalcHalf2( 24 )
            CalcHalf2( 27 )
        End Sub ' Main
           
        ' Half throws a base exception if the input is not even.
        Function Half( input As Integer ) As Integer

            If input Mod 2 <> 0 Then
                Throw New Exception( )
            Else
                Return input / 2
            End If
        End Function ' Half
            
        ' Half2 throws a derived exception if the input is not even.
        Function Half2( input As Integer ) As Integer

            If input Mod 2 <> 0 Then
                Throw New NotEvenException( )
            Else
                Return input / 2
            End If
        End Function ' Half2
            
        ' CalcHalf calls Half and catches any thrown exceptions.
        Sub CalcHalf( input As Integer )

            Try
                Dim halfInput As Integer = Half( input )
                Console.WriteLine( _
                    "Half of {0} is {1}.", input, halfInput )

            Catch ex As Exception
                Console.WriteLine( ex.ToString( ) )
            End Try
        End Sub ' CalcHalf
           
        ' CalcHalf2 calls Half2 and catches any thrown exceptions.
        Sub CalcHalf2( input As Integer )

            Try
                Dim halfInput As Integer = Half2( input )
                Console.WriteLine( _
                    "Half of {0} is {1}.", input, halfInput )

            Catch ex As Exception
                Console.WriteLine( ex.ToString( ) )
            End Try
        End Sub ' CalcHalf2

    End Module ' NewExceptionDemo
End Namespace ' NDP_UE_VB

' 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_VB.NewExceptionDemo.Half(Int32 input)
'    at NDP_UE_VB.NewExceptionDemo.CalcHalf(Int32 input)
' 
' Here, an exception is thrown using the
' parameterless constructor of a derived class.
' 
' Half of 24 is 12.
' NDP_UE_VB.NotEvenException: The argument to a function requiring even input i
' s not divisible by 2.
'    at NDP_UE_VB.NewExceptionDemo.Half2(Int32 input)
'    at NDP_UE_VB.NewExceptionDemo.CalcHalf2(Int32 input)
// 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.
using namespace System;

namespace NDP_UE_CPP
{

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

   };


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


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


   // CalcHalf calls Half and catches any thrown exceptions.
   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.
   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() );
      }

   }

}

int 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" );
   NDP_UE_CPP::CalcHalf( 12 );
   NDP_UE_CPP::CalcHalf( 15 );
   Console::WriteLine( "\nHere, an exception is thrown using the \n"
   "parameterless constructor of a derived class.\n" );
   NDP_UE_CPP::CalcHalf2( 24 );
   NDP_UE_CPP::CalcHalf2( 27 );
}

/*
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_CPP.Half(Int32 input)
   at NDP_UE_CPP.CalcHalf(Int32 input)

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

Half of 24 is 12.
NDP_UE_CPP.NotEvenException: The argument to a function requiring even input is
 not divisible by 2.
   at NDP_UE_CPP.Half2(Int32 input)
   at NDP_UE_CPP.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 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 2.0、1.0

参照

関連項目

Exception クラス
Exception メンバ
System 名前空間