Megjegyzés
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhat bejelentkezni vagy módosítani a címtárat.
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhatja módosítani a címtárat.
Megjegyzés:
Ez a cikk kiegészítő megjegyzéseket tartalmaz az API referenciadokumentációjához.
A hibaüzenetek a kivételt kezelő fejlesztőt célják. A tulajdonság szövegének teljes egészében Message le kell írnia a hibát, és lehetőség szerint azt is meg kell magyaráznia, hogyan kell kijavítani a hibát. A legfelső szintű kivételkezelők megjeleníthetik az üzenetet a végfelhasználóknak, ezért gondoskodnia kell arról, hogy nyelvtanilag helyes legyen, és hogy az üzenet minden mondata ponttal végződjön. Ne használjon kérdőjeleket vagy felkiáltójeleket. Ha az alkalmazás honosított kivételüzeneteket használ, győződjön meg arról, hogy azok megfelelően vannak lefordítva.
Fontos
Ne tegye közzé a bizalmas információkat a kivételüzenetekben a megfelelő engedélyek ellenőrzése nélkül.
A tulajdonság értékét a Message függvény által visszaadott ToStringinformációk tartalmazzák. A Message tulajdonság csak akkor van beállítva, ha létrehoz egy Exception. Ha az aktuális példány konstruktorának nem érkezett üzenet, a rendszer az aktuális rendszerkultúra használatával formázott alapértelmezett üzenetet küld.
Példák
Az alábbi példakód egy Exception kivételt dob, majd elkapja, és a Message tulajdonság használatával megjeleníti a kivétel szöveges üzenetét.
using System;
namespace NDP_UE_CS
{
// Derive an exception; the constructor sets the HelpLink and
// Source properties.
class LogTableOverflowException : Exception
{
const string overflowMessage = "The log table has overflowed.";
public LogTableOverflowException(
string auxMessage, Exception inner) :
base(String.Format("{0} - {1}",
overflowMessage, auxMessage), inner)
{
this.HelpLink = "https://learn.microsoft.com";
this.Source = "Exception_Class_Samples";
}
}
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 e)
{
throw new LogTableOverflowException(
String.Format("Record \"{0}\" was not logged.",
newRecord), e);
}
}
}
class OverflowDemo
{
// Create a log table and force an overflow.
public static void Main()
{
LogTable log = new LogTable(4);
Console.WriteLine(
"This example of \n Exception.Message, \n" +
" Exception.HelpLink, \n Exception.Source, \n" +
" Exception.StackTrace, and \n Exception." +
"TargetSite \ngenerates the following output.");
try
{
for (int count = 1; ; count++)
{
log.AddRecord(
String.Format(
"Log record number {0}", count));
}
}
catch (Exception ex)
{
Console.WriteLine($"{Environment.NewLine}Message ---{Environment.NewLine}{ex.Message}");
Console.WriteLine($"{Environment.NewLine}HelpLink ---{Environment.NewLine}{ex.HelpLink}");
Console.WriteLine($"{Environment.NewLine}Source ---{Environment.NewLine}{ex.Source}");
Console.WriteLine($"{Environment.NewLine}StackTrace ---{Environment.NewLine}{ex.StackTrace}");
Console.WriteLine($"{Environment.NewLine}TargetSite ---{Environment.NewLine}{ex.TargetSite}");
}
}
}
}
/*
This example of
Exception.Message,
Exception.HelpLink,
Exception.Source,
Exception.StackTrace, and
Exception.TargetSite
generates the following output.
Message ---
The log table has overflowed. - Record "Log record number 5" was not logged.
HelpLink ---
https://learn.microsoft.com
Source ---
Exception_Class_Samples
StackTrace ---
at NDP_UE_CS.LogTable.AddRecord(String newRecord)
at NDP_UE_CS.OverflowDemo.Main()
TargetSite ---
Int32 AddRecord(System.String)
*/
// Example for the Exception.HelpLink, Exception.Source,
// Exception.StackTrace, and Exception.TargetSite properties.
open System
let overflowMessage = "The log table has overflowed."
// Derive an exception; the constructor sets the HelpLink and
// Source properties.
type LogTableOverflowException(auxMessage, inner) as this =
inherit Exception($"%s{overflowMessage} - %s{auxMessage}", inner)
do
this.HelpLink <- "https://docs.microsoft.com"
this.Source <- "Exception_Class_Samples"
type LogTable(numElements) =
let logArea = Array.zeroCreate<string> numElements
let mutable elemInUse = 0
// The AddRecord method throws a derived exception if
// the array bounds exception is caught.
member this.AddRecord(newRecord) =
try
logArea[elemInUse] <- newRecord
elemInUse <- elemInUse + 1
elemInUse - 1
with e ->
raise (LogTableOverflowException($"Record \"{newRecord}\" was not logged.", e) )
// Create a log table and force an overflow.
let log = LogTable 4
printfn
"""This example of
Exception.Message,
Exception.HelpLink,
Exception.Source,
Exception.StackTrace, and
Exception.TargetSite
generates the following output."""
try
for count = 1 to 1000000 do
log.AddRecord $"Log record number {count}"
|> ignore
with ex ->
printfn $"\nMessage ---\n{ex.Message}"
printfn $"\nHelpLink ---\n{ex.HelpLink}"
printfn $"\nSource ---\n{ex.Source}"
printfn $"\nStackTrace ---\n{ex.StackTrace}"
printfn $"\nTargetSite ---\n{ex.TargetSite}"
// This example of
// Exception.Message,
// Exception.HelpLink,
// Exception.Source,
// Exception.StackTrace, and
// Exception.TargetSite
// generates the following output.
// Message ---
// The log table has overflowed. - Record "Log record number 5" was not logged.
// HelpLink ---
// https://docs.microsoft.com
// Source ---
// Exception_Class_Samples
// StackTrace ---
// at NDP_UE_FS.LogTable.AddRecord(String newRecord)
// at <StartupCode$fs>.$NDP_UE_FS.main@()
// TargetSite ---
// Int32 AddRecord(System.String)
' Example for the Exception.HelpLink, Exception.Source,
' Exception.StackTrace, and Exception.TargetSite properties.
Namespace NDP_UE_VB
' Derive an exception; the constructor sets the HelpLink and
' Source properties.
Class LogTableOverflowException
Inherits Exception
Private Const overflowMessage As String = _
"The log table has overflowed."
Public Sub New( auxMessage As String, inner As Exception )
MyBase.New( String.Format( "{0} - {1}", _
overflowMessage, auxMessage ), inner )
Me.HelpLink = "https://docs.microsoft.com"
Me.Source = "Exception_Class_Samples"
End Sub
End Class
Class LogTable
Public Sub New(numElements As Integer)
logArea = New String(numElements) {}
elemInUse = 0
End Sub
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
Module OverflowDemo
' Create a log table and force an overflow.
Sub Main( )
Dim log As New LogTable( 4 )
Console.WriteLine( "This example of " & vbCrLf & _
" Exception.Message, " & vbCrLf & _
" Exception.HelpLink, " & vbCrLf & _
" Exception.Source, " & vbCrLf & _
" Exception.StackTrace, and " & vbCrLf & _
" Exception.TargetSite " & vbCrLf & _
"generates the following output." )
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( vbCrLf & _
"Message ---" & vbCrLf & ex.Message )
Console.WriteLine( vbCrLf & _
"HelpLink ---" & vbCrLf & ex.HelpLink )
Console.WriteLine( vbCrLf & _
"Source ---" & vbCrLf & ex.Source )
Console.WriteLine( vbCrLf & _
"StackTrace ---" & vbCrLf & ex.StackTrace )
Console.WriteLine( vbCrLf & "TargetSite ---" & _
vbCrLf & ex.TargetSite.ToString( ) )
End Try
End Sub
End Module ' OverflowDemo
End Namespace ' NDP_UE_VB
' This example of
' Exception.Message,
' Exception.HelpLink,
' Exception.Source,
' Exception.StackTrace, and
' Exception.TargetSite
' generates the following output.
'
' Message ---
' The log table has overflowed. - Record "Log record number 5" was not logged.
'
' HelpLink ---
' https://docs.microsoft.com
'
' Source ---
' Exception_Class_Samples
'
' StackTrace ---
' at NDP_UE_VB.LogTable.AddRecord(String newRecord)
' at NDP_UE_VB.OverflowDemo.Main()
'
' TargetSite ---
' Int32 AddRecord(System.String)