Exception.InnerException Tulajdonság
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Lekéri az Exception aktuális kivételt okozó példányt.
public:
property Exception ^ InnerException { Exception ^ get(); };
public Exception InnerException { get; }
public Exception? InnerException { get; }
member this.InnerException : Exception
Public ReadOnly Property InnerException As Exception
Tulajdonság értéke
Az aktuális kivételt okozó hibát leíró objektum. A InnerException tulajdonság ugyanazt az értéket adja vissza, mint amelyet a Exception(String, Exception) konstruktornak átadott, vagy null ha a belső kivételértéket nem adta meg a konstruktornak. Ez a tulajdonság írásvédett.
Megvalósítás
Példák
Az alábbi példa egy belső kivételre hivatkozó kivétel dobását és elfogását mutatja be.
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()
Megjegyzések
Ha egy korábbi kivétel XYközvetlen eredményeként kivételt ad ki, a InnerException tulajdonságnak tartalmaznia X kell egy hivatkozást Y.
InnerException A tulajdonság használatával szerezze be az aktuális kivételhez vezető kivételek készletét.
Létrehozhat egy új kivételt, amely egy korábbi kivételt fog. A második kivételt kezelő kód a korábbi kivétel további információit is felhasználhatja a hiba megfelelőbb kezeléséhez.
Tegyük fel, hogy van egy függvény, amely beolvassa a fájlt, és formázja az adatokat a fájlból. Ebben a példában, amikor a kód megpróbálja beolvasni a fájlt, a rendszer egy IOException hibát ad ki. A függvény elkapja a IOException és dob egy FileNotFoundException. Ez IOException menthető a InnerException tulajdonságban FileNotFoundException, így a kód, amely elkapja a FileNotFoundException kezdeti hiba okának vizsgálatához.
A InnerException belső kivételre hivatkozó tulajdonság a kivételobjektum inicializálásakor van beállítva.