DefaultTraceListener.Fail Method

Definition

Emits or displays a message and a stack trace for an assertion that always fails.

Overloads

Fail(String)

Emits or displays a message and a stack trace for an assertion that always fails.

Fail(String, String)

Emits or displays detailed messages and a stack trace for an assertion that always fails.

Fail(String)

Source:
DefaultTraceListener.cs
Source:
DefaultTraceListener.cs
Source:
DefaultTraceListener.cs

Emits or displays a message and a stack trace for an assertion that always fails.

C#
public override void Fail(string? message);
C#
public override void Fail(string message);

Parameters

message
String

The message to emit or display.

Examples

The following code example writes an error message to a DefaultTraceListener using the Fail(String) method. The method also writes the message to the console if a user interface is not available.

C#
// Report that the required argument is not present.
const string ENTER_PARAM = "Enter the number of " +
          "possibilities as a command line argument.";
defaultListener.Fail(ENTER_PARAM);
if (!defaultListener.AssertUiEnabled)
{
    Console.WriteLine(ENTER_PARAM);
}

Remarks

By default, this method sends the message parameter and a stack trace to a message box (when the application runs in user-interface mode) and to the DefaultTraceListener instance.

See also

Applies to

.NET 10 and other versions
Product Versions
.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, 10
.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 2.0, 2.1

Fail(String, String)

Source:
DefaultTraceListener.cs
Source:
DefaultTraceListener.cs
Source:
DefaultTraceListener.cs

Emits or displays detailed messages and a stack trace for an assertion that always fails.

C#
public override void Fail(string? message, string? detailMessage);
C#
public override void Fail(string message, string detailMessage);

Parameters

message
String

The message to emit or display.

detailMessage
String

The detailed message to emit or display.

Examples

The following code example calls a function that calls the Fail(String, String) method to log a detailed error message if the function throws an exception. The method writes the message to the console if a user interface is not available.

C#
// Compute the next binomial coefficient and handle all exceptions.
try
{
    result = CalcBinomial(possibilities, iter);
}
catch(Exception ex)
{
    string failMessage = String.Format("An exception was raised when " +
        "calculating Binomial( {0}, {1} ).", possibilities, iter);
    defaultListener.Fail(failMessage, ex.Message);
    if (!defaultListener.AssertUiEnabled)
    {
        Console.WriteLine(failMessage+ "\n" +ex.Message);
    }
    return;
}

Remarks

By default, this method sends the message parameter, the detailMessage parameter, and a stack trace to a message box (when the application runs in user-interface mode) and to the DefaultTraceListener instance.

See also

Applies to

.NET 10 and other versions
Product Versions
.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, 10
.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 2.0, 2.1