Exception.InnerException Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the Exception instance that caused the current exception.
public:
property Exception ^ InnerException { Exception ^ get(); };
public Exception InnerException { get; }
public Exception? InnerException { get; }
member this.InnerException : Exception
Public ReadOnly Property InnerException As Exception
An object that describes the error that caused the current exception. The InnerException property returns the same value as was passed into the Exception(String, Exception) constructor, or null
if the inner exception value was not supplied to the constructor. This property is read-only.
The following example demonstrates throwing and catching an exception that references an inner exception.
using namespace System;
public ref class AppException: public Exception
{
public:
AppException(String^ message ) : Exception(message)
{}
AppException(String^ message, Exception^ inner) : Exception(message, inner)
{}
};
public ref class Example
{
public:
void ThrowInner()
{
throw gcnew AppException("Exception in ThrowInner method.");
}
void CatchInner()
{
try {
this->ThrowInner();
}
catch (AppException^ e) {
throw gcnew AppException("Error in CatchInner caused by calling the ThrowInner method.", e);
}
}
};
int main()
{
Example^ ex = gcnew Example();
try {
ex->CatchInner();
}
catch (AppException^ e) {
Console::WriteLine("In catch block of Main method.");
Console::WriteLine("Caught: {0}", e->Message);
if (e->InnerException != nullptr)
Console::WriteLine("Inner exception: {0}", e->InnerException);
}
}
// The example displays the following output:
// In catch block of Main method.
// Caught: Error in CatchInner caused by calling the ThrowInner method.
// Inner exception: AppException: Exception in ThrowInner method.
// at Example.ThrowInner()
// at Example.CatchInner()
using System;
public class AppException : Exception
{
public AppException(String message) : base (message)
{}
public AppException(String message, Exception inner) : base(message,inner) {}
}
public class Example
{
public static void Main()
{
Example ex = new Example();
try {
ex.CatchInner();
}
catch(AppException e) {
Console.WriteLine ("In catch block of Main method.");
Console.WriteLine("Caught: {0}", e.Message);
if (e.InnerException != null)
Console.WriteLine("Inner exception: {0}", e.InnerException);
}
}
public void ThrowInner ()
{
throw new AppException("Exception in ThrowInner method.");
}
public void CatchInner()
{
try {
this.ThrowInner();
}
catch (AppException e) {
throw new AppException("Error in CatchInner caused by calling the ThrowInner method.", e);
}
}
}
// The example displays the following output:
// In catch block of Main method.
// Caught: Error in CatchInner caused by calling the ThrowInner method.
// Inner exception: AppException: Exception in ThrowInner method.
// at Example.ThrowInner()
// at Example.CatchInner()
open System
type AppException =
inherit Exception
new (message: string) = { inherit Exception(message) }
new (message: string, inner) = { inherit Exception(message, inner) }
let throwInner () =
raise (AppException "Exception in throwInner function.")
()
let catchInner () =
try
throwInner ()
with :? AppException as e ->
raise (AppException("Error in catchInner caused by calling the throwInner function.", e) )
[<EntryPoint>]
let main _ =
try
catchInner ()
with :? AppException as e ->
printfn "In with block of main function."
printfn $"Caught: {e.Message}"
if e.InnerException <> null then
printfn $"Inner exception: {e.InnerException}"
0
// The example displays the following output:
// In with block of main function.
// Caught: Error in catchInner caused by calling the throwInner function.
// Inner exception: Example+AppException: Exception in throwInner function.
// at Example.throwInner()
// at Example.catchInner()
Public Class AppException : Inherits Exception
Public Sub New(message As String)
MyBase.New(message)
End Sub
Public Sub New(message As String, inner As Exception)
MyBase.New(message, inner)
End Sub
End Class
Public Class Example
Public Shared Sub Main()
Dim testInstance As New Example()
Try
testInstance.CatchInner()
Catch e As AppException
Console.WriteLine ("In catch block of Main method.")
Console.WriteLine("Caught: {0}", e.Message)
If e.InnerException IsNot Nothing Then
Console.WriteLine("Inner exception: {0}", e.InnerException)
End If
End Try
End Sub
Public Sub ThrowInner()
Throw New AppException("Exception in ThrowInner method.")
End Sub
Public Sub CatchInner()
Try
Me.ThrowInner()
Catch e As AppException
Throw New AppException("Error in CatchInner caused by calling the ThrowInner method.", e)
End Try
End Sub
End Class
' The example displays the following output:
' In catch block of Main method.
' Caught: Error in CatchInner caused by calling the ThrowInner method.
' Inner exception: AppException: Exception in ThrowInner method.
' at Example.ThrowInner()
' at Example.CatchInner()
When an exception X
is thrown as a direct result of a previous exception Y
, the InnerException property of X
should contain a reference to Y
.
Use the InnerException property to obtain the set of exceptions that led to the current exception.
You can create a new exception that catches an earlier exception. The code that handles the second exception can make use of the additional information from the earlier exception to handle the error more appropriately.
Suppose that there is a function that reads a file and formats the data from that file. In this example, as the code tries to read the file, an IOException is thrown. The function catches the IOException and throws a FileNotFoundException. The IOException could be saved in the InnerException property of the FileNotFoundException, enabling the code that catches the FileNotFoundException to examine the cause of the initial error.
The InnerException property, which holds a reference to the inner exception, is set upon initialization of the exception object.
제품 | 버전 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET 피드백
.NET은(는) 오픈 소스 프로젝트입니다. 다음 링크를 선택하여 피드백을 제공해 주세요.