次の方法で共有


Exception コンストラクタ (String, Exception)

指定したエラー メッセージと、この例外の原因である内部例外への参照を使用して、Exception クラスの新しいインスタンスを初期化します。

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

構文

'宣言
Public Sub New ( _
    message As String, _
    innerException As Exception _
)
'使用
Dim message As String
Dim innerException As Exception

Dim instance As New Exception(message, innerException)
public Exception (
    string message,
    Exception innerException
)
public:
Exception (
    String^ message, 
    Exception^ innerException
)
public Exception (
    String message, 
    Exception innerException
)
public function Exception (
    message : String, 
    innerException : Exception
)

パラメータ

  • message
    例外の原因を説明するエラー メッセージ。
  • innerException
    現在の例外の原因である例外。内部例外が指定されていない場合は、null 参照 (Visual Basic の場合は Nothing)。

解説

前の例外の直接の結果としてスローされる例外については、InnerException プロパティに、前の例外への参照が格納されます。InnerException プロパティは、コンストラクタに渡されたものと同じ値を返します。InnerException プロパティによって内部例外値がコンストラクタに渡されなかった場合は、null 参照 (Visual Basic の場合は Nothing) を返します。

Exception のインスタンスの初期プロパティ値を次の表に示します。

プロパティ

InnerException

内部例外参照。

Message

エラー メッセージ文字列。

使用例

特定の条件の Exception を派生させるコード例を次に示します。この例では、基本 Exception クラスと派生クラスの両方について、メッセージと内部例外をパラメータとして受け取るコンストラクタの使用方法を示します。

' Sample for Exception( String, Exception ) constructor.
Imports System
Imports Microsoft.VisualBasic

Namespace NDP_UE_VB

    ' Derive an exception with a specifiable message and inner exception.
    Class LogTableOverflowException
        Inherits Exception

        Private Const overflowMessage As String = _
            "The log table has overflowed."
           
        Public Sub New( )
            MyBase.New( overflowMessage )
        End Sub ' New
           
        Public Sub New( auxMessage As String )
            MyBase.New( String.Format( "{0} - {1}", _
                overflowMessage, auxMessage ) )
        End Sub ' New
           
        Public Sub New( auxMessage As String, inner As Exception )
            MyBase.New( String.Format( "{0} - {1}", _
                overflowMessage, auxMessage ), inner )
        End Sub ' New
    End Class ' LogTableOverflowException

    Class LogTable
       
        Public Sub New( numElements As Integer )
            logArea = New String( numElements ) { }
            elemInUse = 0
        End Sub ' New
           
        Protected logArea( ) As String
        Protected elemInUse As Integer
           
        ' The AddRecord method throws a derived exception 
        ' if the array bounds exception is caught.
        Public Function AddRecord( newRecord As String ) As Integer

            Try
                Dim curElement as Integer = elemInUse
                logArea( elemInUse ) = newRecord
                elemInUse += 1
                Return curElement

            Catch ex As Exception
                Throw New LogTableOverflowException( String.Format( _
                    "Record ""{0}"" was not logged.", newRecord ), ex )
            End Try
        End Function ' AddRecord
        End Class ' LogTable

        Module OverflowDemo
           
        ' Create a log table and force an overflow.
        Sub Main()
            Dim log As New LogTable(4)
              
            Console.WriteLine( _
                "This example of the Exception( String, Exception )" & _
                vbCrLf & "constructor generates the following output." )
            Console.WriteLine( vbCrLf & _
                "Example of a derived exception " & vbCrLf & _
                "that references an inner exception:" & vbCrLf )
            Try
                Dim count As Integer = 0
                 
                Do
                    log.AddRecord( _
                        String.Format( _
                            "Log record number {0}", count ) )
                    count += 1
                Loop

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

    End Module ' OverflowDemo
End Namespace ' NDP_UE_VB

' This example of the Exception( String, Exception )
' constructor generates the following output.
' 
' Example of a derived exception
' that references an inner exception:
' 
' NDP_UE_VB.LogTableOverflowException: The log table has overflowed. - Record "
' Log record number 5" was not logged. ---> System.IndexOutOfRangeException: In
' dex was outside the bounds of the array.
'    at NDP_UE_VB.LogTable.AddRecord(String newRecord)
'    --- End of inner exception stack trace ---
'    at NDP_UE_VB.LogTable.AddRecord(String newRecord)
'    at NDP_UE_VB.OverflowDemo.Main()
// Example for the Exception( string, Exception ) constructor.
using System;

namespace NDP_UE_CS
{
    // Derive an exception with a specifiable message and inner exception.
    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 LogTable
    {
        public LogTable( int numElements )
        {
            logArea = new string[ numElements ];
            elemInUse = 0;
        }

        protected string[ ] logArea;
        protected int       elemInUse;

        // The AddRecord method throws a derived exception 
        // if the array bounds exception is caught.
        public    int       AddRecord( string newRecord )
        {
            try
            {
                logArea[ elemInUse ] = newRecord;
                return elemInUse++;
            }
            catch( Exception ex )
            {
                throw new LogTableOverflowException( 
                    String.Format( "Record \"{0}\" was not logged.", 
                        newRecord ), ex );
            }
        }
    }

    class OverflowDemo 
    {
        // Create a log table and force an overflow.
        public static void Main() 
        {
            LogTable log = new LogTable( 4 );

            Console.WriteLine( 
                "This example of the Exception( string, Exception )" +
                "\nconstructor generates the following output." );
            Console.WriteLine( 
                "\nExample of a derived exception " +
                "that references an inner exception:\n" );
            try
            {
                for( int count = 1; ; count++ )
                {
                    log.AddRecord( 
                        String.Format( 
                            "Log record number {0}", count ) );
                }
            }
            catch( Exception ex )
            {
                Console.WriteLine( ex.ToString( ) );
            }
        }
    }
}

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

Example of a derived exception that references an inner exception:

NDP_UE_CS.LogTableOverflowException: The log table has overflowed. - Record "Lo
g record number 5" was not logged. ---> System.IndexOutOfRangeException: Index
was outside the bounds of the array.
   at NDP_UE_CS.LogTable.AddRecord(String newRecord)
   --- End of inner exception stack trace ---
   at NDP_UE_CS.LogTable.AddRecord(String newRecord)
   at NDP_UE_CS.OverflowDemo.Main()
*/
// Example for the Exception( String*, Exception* ) constructor.
using namespace System;

namespace NDP_UE_CPP
{

   // Derive an exception with a specifiable message and inner exception.
   public ref class LogTableOverflowException: public Exception
   {
   private:
      static String^ overflowMessage =  "The log table has overflowed.";

   public:
      LogTableOverflowException()
         : Exception( overflowMessage )
      {}

      LogTableOverflowException( String^ auxMessage )
         : Exception( String::Format( "{0} - {1}", overflowMessage, auxMessage ) )
      {}

      LogTableOverflowException( String^ auxMessage, Exception^ inner )
         : Exception( String::Format( "{0} - {1}", overflowMessage, auxMessage ), inner )
      {}

   };

   public ref class LogTable
   {
   public:
      LogTable( int numElements )
      {
         logArea = gcnew array<String^>(numElements);
         elemInUse = 0;
      }


   protected:
      array<String^>^logArea;
      int elemInUse;

   public:

      // The AddRecord method throws a derived exception 
      // if the array bounds exception is caught.
      int AddRecord( String^ newRecord )
      {
         try
         {
            logArea[ elemInUse ] = newRecord;
            return elemInUse++;
         }
         catch ( Exception^ ex ) 
         {
            throw gcnew LogTableOverflowException( String::Format( "Record \"{0}\" was not logged.", newRecord ),ex );
         }

      }

   };


   // Create a log table and force an overflow.
   void ForceOverflow()
   {
      LogTable^ log = gcnew LogTable( 4 );
      try
      {
         for ( int count = 1; ; count++ )
         {
            log->AddRecord( String::Format( "Log record number {0}", count ) );

         }
      }
      catch ( Exception^ ex ) 
      {
         Console::WriteLine( ex->ToString() );
      }

   }

}

int main()
{
   Console::WriteLine( "This example of the Exception( String*, Exception* )\n"
   "constructor generates the following output." );
   Console::WriteLine( "\nExample of a derived exception "
   "that references an inner exception:\n" );
   NDP_UE_CPP::ForceOverflow();
}

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

Example of a derived exception that references an inner exception:

NDP_UE_CPP.LogTableOverflowException: The log table has overflowed. - Record "L
og record number 5" was not logged. ---> System.IndexOutOfRangeException: Index
 was outside the bounds of the array.
   at NDP_UE_CPP.LogTable.AddRecord(String newRecord)
   --- End of inner exception stack trace ---
   at NDP_UE_CPP.LogTable.AddRecord(String newRecord)
   at NDP_UE_CPP.ForceOverflow()
*/
// Example for the Exception( string, Exception ) constructor.
package NDP_UE_JSL; 

import System.* ;

// Derive an exception with a specifiable message and inner exception.
class LogTableOverflowException extends System.Exception
{
    private String overflowMessage = "The log table has overflowed.";

    public LogTableOverflowException()
    {
        super("The log table has overflowed.");
    } //LogTableOverflowException

    public LogTableOverflowException(String auxMessage)
    {
        super(String.Format("The log table has overflowed. - {0}", 
            auxMessage));
    } //LogTableOverflowException

    public LogTableOverflowException(String auxMessage, Exception inner)
    {
        super(String.Format("The log table has overflowed.- {0}", auxMessage), 
            inner);
    } //LogTableOverflowException
} //LogTableOverflowException

class LogTable
{
    public LogTable(int numElements)
    {
        logArea = new String[numElements];
        elemInUse = 0;
    } //LogTable

    protected String logArea[];
    protected int elemInUse;

    // The AddRecord method throws a derived exception 
    // if the array bounds exception is caught.
    public int AddRecord(String newRecord) throws LogTableOverflowException
    {
        try {
            logArea.set_Item(elemInUse, newRecord);
            return elemInUse++;
        }
        catch (Exception ex) {
            throw new LogTableOverflowException(String.Format(
                    "Record \"{0}\" was not logged.", newRecord), ex);
        }
    } //AddRecord
} //LogTable

class OverflowDemo
{
    // Create a log table and force an overflow.
    public static void main(String[] args)
    {
        LogTable log = new LogTable(4);

        Console.WriteLine(("This example of the Exception( string, Exception )"
            + "\nconstructor generates the following output."));
        Console.WriteLine(("\nExample of a derived exception " + 
            "that references an inner exception:\n"));
        try {
            for(int iCtr = 1; ; iCtr++) {
                log.AddRecord(String.Format("Log record number {0}", 
                    System.Convert.ToString(iCtr)));
            }
        }
        catch (System.Exception ex) {
            Console.WriteLine(ex.toString());
        }
    } //main
} //OverflowDemo
 
/*
This example of the Exception( string, Exception )
constructor generates the following output.

Example of a derived exception that references an inner exception:

NDP_UE_JSL.LogTableOverflowException: The log table has overflowed.- 
Record "Log record number 5" was not logged. ---> 
java.lang.ArrayIndexOutOfBoundsException:
 Index was outside the bounds of the array.
   --- End of inner exception stack trace ---
   at NDP_UE_JSL.LogTable.AddRecord(String newRecord) in C:\
Documents and Settings\My Documents\Visual Studio Projects\ConsoleApp - JS\
ConsoleApp - JS\Class1.jsl:line 52
   at NDP_UE_JSL.OverflowDemo.main(String[] args) in C:\Documents and Settings\
My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleApp - JS\
Class1.jsl:line 71
*/

プラットフォーム

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 名前空間