Exception.InnerException プロパティ
アセンブリ: mscorlib (mscorlib.dll 内)

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

ある例外 X が先に発生した例外 Y の直接の結果としてスローされるとき、X の InnerException プロパティには Y への参照が格納されます。
InnerException プロパティを使用して、現在の例外の原因になった例外のセットを取得します。
以前の例外をキャッチする新しい例外を作成できます。後に発生した例外を処理するコードは、先に発生した例外からの追加情報を使用して、より的確にエラーを処理できます。
ファイルを読み取り、そのファイルのデータの書式を指定する関数があるとします。この例では、ファイルの読み取りを試みるコードとして、IOException がスローされます。この関数は、IOException を受け取り、FileNotFoundException をスローします。IOException を FileNotFoundException の InnerException プロパティに保存でき、それにより、初期エラーの原因を検査するための FileNotFoundException を受け取るコードが有効になります。

内部例外を参照する例外をスローしたり受け取る例を次に示します。
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

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


_Exception.InnerException プロパティ
アセンブリ: mscorlib (mscorlib.dll 内)

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

このメソッドは、アンマネージ コードからマネージ クラスにアクセスするためのメソッドであるため、マネージ コードからは呼び出さないでください。
Exception.InnerException プロパティは、現在の例外を発生させた Exception インスタンスを取得します。

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- Exception.InnerExceptionのページへのリンク