Exception.InnerExceptionとは? わかりやすく解説

Exception.InnerException プロパティ

現在の例外を発生させた Exception インスタンス取得します

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

解説解説

ある例外 X先に発生した例外 Y直接結果としてスローされるとき、XInnerException プロパティには Y への参照格納されます。

InnerException プロパティ使用して現在の例外の原因になった例外セット取得します

以前例外キャッチする新し例外作成できます。後に発生した例外処理するコードは、先に発生した例外からの追加情報使用して、より的確にエラーを処理できます

ファイル読み取り、そのファイルデータ書式指定する関数があるとします。この例では、ファイル読み取り試みコードとして、IOException がスローさます。この関数は、IOException受け取り、FileNotFoundException をスローます。IOExceptionFileNotFoundExceptionInnerException プロパティ保存でき、それにより、初期エラー原因検査するための FileNotFoundException受け取コード有効になります

内部例外への参照保持する InnerException プロパティは、例外オブジェクト初期化時に設定されます。

使用例使用例

内部例外参照する例外スローしたり受け取る例を次に示します

Imports System

Public Class MyAppException
   Inherits ApplicationException
   
   Public Sub New(message
 As [String])
      MyBase.New(message)
   End Sub 'New
   
   Public Sub New(message
 As [String], inner As Exception)
      MyBase.New(message, inner)
   End Sub 'New
End Class 'MyAppException

Public Class ExceptExample
   
   Public Sub ThrowInner()
      Throw New MyAppException("ExceptExample
 inner exception")
   End Sub 'ThrowInner
   
   Public Sub CatchInner()
      Try
         Me.ThrowInner()
      Catch e As Exception
         Throw New MyAppException("Error
 caused by trying ThrowInner.", e)
      End Try
   End Sub 'CatchInner
End Class 'ExceptExample

Public Class Test
   
   Public Shared Sub Main()
      Dim testInstance As New
 ExceptExample()
      Try
         testInstance.CatchInner()
      Catch e As Exception
         Console.WriteLine("In Main catch block. Caught: {0}",
 e.Message)
         Console.WriteLine("Inner Exception is {0}",
 e.InnerException)
      End Try
   End Sub 'Main
End Class 'Test
using System;
public class MyAppException:ApplicationException
 
{
   public MyAppException (String message) : base
 (message) 
   {}
   public MyAppException (String message, Exception inner) : base(message,inner)
 {}    
   }
public class ExceptExample 
{
   public void ThrowInner () 
   {
   throw new MyAppException("ExceptExample inner exception");
   }
   public void CatchInner() 
   {
      try 
      {
      this.ThrowInner();
      }
         catch (Exception e) 
         {
         throw new MyAppException("Error caused by trying
 ThrowInner.",e);
         }
      }
   }
public class Test 
{
   public static void Main()
 
   {
   ExceptExample testInstance = new ExceptExample();
      try 
      {
      testInstance.CatchInner();
      }
         catch(Exception e) 
         {
         Console.WriteLine ("In Main catch block. Caught:
 {0}", e.Message);
         Console.WriteLine ("Inner Exception is {0}",e.InnerException);
         }
      }
}
using namespace System;
public ref class MyAppException: public
 ApplicationException
{
public:
   MyAppException( String^ message )
      : ApplicationException( message )
   {}

   MyAppException( String^ message, Exception^ inner )
      : ApplicationException( message, inner )
   {}

};

public ref class ExceptExample
{
public:
   void ThrowInner()
   {
      throw gcnew MyAppException( "ExceptExample inner exception" );
   }

   void CatchInner()
   {
      try
      {
         this->ThrowInner();
      }
      catch ( Exception^ e ) 
      {
         throw gcnew MyAppException( "Error caused by trying ThrowInner.",e
 );
      }

   }

};

int main()
{
   ExceptExample^ testInstance = gcnew ExceptExample;
   try
   {
      testInstance->CatchInner();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "In Main catch block. Caught: {0}",
 e->Message );
      Console::WriteLine( "Inner Exception is {0}", e->InnerException
 );
   }

}

import System.*;
public class MyAppException extends ApplicationException
{
    public MyAppException(String message)
    {
      super(message);
    } //MyAppException

    public MyAppException(String message, System.Exception inner)
    {
      super(message, inner);
    } //MyAppException
} //MyAppException

public class ExceptExample
{
    public void ThrowInner()throws MyAppException
    {
        throw new MyAppException("ExceptExample inner exception");
    } //ThrowInner

    public void CatchInner()throws MyAppException
    {
        try {
            this.ThrowInner();
        }
        catch (System.Exception e) {
            throw new MyAppException("Error caused by trying
 ThrowInner.", e);
        }
    } //CatchInner
} //ExceptExample

public class Test
{
    public static void main(String[]
 args)
    {
        ExceptExample testInstance = new ExceptExample();
        try {
            testInstance.CatchInner();
        }
        catch (System.Exception e) {
            Console.WriteLine("In main catch block. Caught:
 {0}", 
                e.get_Message());
            Console.WriteLine("Inner Exception is {0}", 
                e.get_InnerException());
        }
    } //main
} //Test

このコードによって、次の出力生成されます。

In Main
  catch block. Caught: Error caused by trying ThrowInner. Inner Exception is
  MyAppException: ExceptExample inner exception at ExceptExample.ThrowInner() at
  ExceptExample.CatchInner()
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

_Exception.InnerException プロパティ

メモ : このプロパティは、.NET Framework version 2.0新しく追加されたものです。

COM オブジェクトに、Exception.InnerException プロパティへのバージョン依存しないアクセス用意されています。

このプロパティは、CLS準拠していません。  

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

Exception InnerException { get; }
property Exception^ InnerException {
    Exception^ get ();
}
/** @property */
Exception get_InnerException ()
function get InnerException () : Exception

プロパティ
現在の例外を発生させたエラー説明する System.Exception のインスタンスException.InnerException プロパティは、コンストラクタ渡されたものと同じ値を返します。ただし、内部例外の値がコンストラクタ渡されなかった場合は、null 参照 (Visual Basic の場合Nothing) を返します。このプロパティ読み取り専用です。

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
_Exception インターフェイス
_Exception メンバ
System.Runtime.InteropServices 名前空間
Exception.InnerException



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Exception.InnerException」の関連用語

Exception.InnerExceptionのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Exception.InnerExceptionのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS