Export (0) Print
Expand All

Debug.Fail Method

Emits an error message.

Overload List

Emits the specified error message.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Sub Fail(String)
[C#] public static void Fail(string);
[C++] public: static void Fail(String*);
[JScript] public static function Fail(String);

Emits an error message and a detailed error message.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Sub Fail(String, String)
[C#] public static void Fail(string, string);
[C++] public: static void Fail(String*, String*);
[JScript] public static function Fail(String, String);

Example

The following example uses the Fail method to print a message during exception handling.

[Visual Basic] 
Catch e As Exception
    Debug.Fail("Invalid value: " + value.ToString(), "Resetting value to newValue.")
    value = newValue
End Try

[C#] 
catch (Exception) {
    Debug.Fail("Invalid value: " + value.ToString(), 
       "Resetting value to newValue.");
    value = newValue;
 }

[C++] 
try{
    Object * b;
    b->ToString();
}
catch (Exception * e) {
    Debug::Fail(S"Cannot find SpecialController, proceeding with StandardController", S"Setting Controller to default value");
    controller = DEFAULT;
}

[JScript] 
catch (ex : Exception) {
    Debug.Fail("Invalid value: " + value.ToString(), 
       "Resetting value to newValue.");
    value = newValue;
 }

You can also use the Fail method in a switch statement.

[Visual Basic] 
Select Case option1
    Case MyOption.First
        result = 1.0
    
    ' Insert additional cases.
    Case Else
        Debug.Fail("Unknown Option " & option1, "Result set to 1.0")
        result = 1.0
End Select

[C#] 
switch (option1) {
    case MyOption.First:
       result = 1.0;
       break;
 
    // Insert additional cases.
 
    default:
       Debug.Fail("Unknown Option " + option1, "Result set to 1.0");
       result = 1.0;
       break;
 }

[C++] 
__value enum Option { First, Second, Last };

//...

int option = Option::Last + 1;

switch (option) {
    case Option::First:
    case Option::Second:
    case Option::Last:
    result = 1;
    break;

    // Insert additional cases.
    default:
        Debug::Fail(String::Concat(S"Unknown Option", __box(option)));
        result = -1;
        break;
    }

[JScript] 
switch (option1) {
    case MyOption.First:
       result = 1.0;
       break;
 
    // Insert additional cases.
 
    default:
       Debug.Fail("Unknown Option " + option1, "Result set to 1.0");
       result = 1.0;
       break;
 }

See Also

Debug Class | Debug Members | System.Diagnostics Namespace

Show:
© 2014 Microsoft