Model a test that expects an exception for one input shape.

testing Exception tests pass when the expected exception is observed and fail when the code completes unexpectedly.

Expected Exception

denominator
ExpectedException.cs
Replay: real traced execution (multi-file project)
using System;

class Program
{
    static int Divide(int numerator, int denominator)
    {
        return numerator / denominator;
    }

    static void Main()
    {
        int denominator = 0;
        bool expectedException = denominator == 0;
        bool sawException = false;
        string outcome = "completed";

        try
        {
            int value = Divide(10, denominator);
            outcome = $"value={value}";
        }
        catch (DivideByZeroException)
        {
            sawException = true;
            outcome = "divide-by-zero";
        }

        bool testPassed = sawException == expectedException;

        Console.WriteLine($"expectedException={expectedException}");
        Console.WriteLine($"sawException={sawException}");
        Console.WriteLine($"outcome={outcome}");
        Console.WriteLine($"testPassed={testPassed}");
    }
}
using System;

class Program
{
    static int Divide(int numerator, int denominator)
    {
        return numerator / denominator;
    }

    static void Main()
    {
        int denominator = 2;
        bool expectedException = denominator == 0;
        bool sawException = false;
        string outcome = "completed";

        try
        {
            int value = Divide(10, denominator);
            outcome = $"value={value}";
        }
        catch (DivideByZeroException)
        {
            sawException = true;
            outcome = "divide-by-zero";
        }

        bool testPassed = sawException == expectedException;

        Console.WriteLine($"expectedException={expectedException}");
        Console.WriteLine($"sawException={sawException}");
        Console.WriteLine($"outcome={outcome}");
        Console.WriteLine($"testPassed={testPassed}");
    }
}
using System;

class Program
{
    static int Divide(int numerator, int denominator)
    {
        return numerator / denominator;
    }

    static void Main()
    {
        int denominator = 5;
        bool expectedException = denominator == 0;
        bool sawException = false;
        string outcome = "completed";

        try
        {
            int value = Divide(10, denominator);
            outcome = $"value={value}";
        }
        catch (DivideByZeroException)
        {
            sawException = true;
            outcome = "divide-by-zero";
        }

        bool testPassed = sawException == expectedException;

        Console.WriteLine($"expectedException={expectedException}");
        Console.WriteLine($"sawException={sawException}");
        Console.WriteLine($"outcome={outcome}");
        Console.WriteLine($"testPassed={testPassed}");
    }
}
  1. denominator ← 0, expectedException ← True, sawException ← False

    10static void Main()11{12    int denominator→ 0 = 0; //@denominator=2, 513    bool expectedException→ True = denominator0 == 0;14    bool sawException→ False = false;15    string outcome→ completed = "completed";
  2. try

    17try18{19    int value = Divide(10, denominator0);20    outcome = $"value={value}";
  3. static int Divide(int numerator, int denominator)

    4{5    static int Divide(int numerator10, int denominator0)6    {7        return numerator10 / denominator0;8    }
  4. sawException ← True, outcome ← divide-by-zero

    21}22catch (DivideByZeroException)23{24    sawException→ True = true;25    outcome→ divide-by-zero = "divide-by-zero";26}
  5. testPassed ← True

    28    bool testPassed→ True = sawExceptionTrue == expectedExceptionTrue;2930    Console.WriteLine($"expectedException={expectedExceptionTrue}");31    Console.WriteLine($"sawException={sawExceptionTrue}");32    Console.WriteLine($"outcome={outcomedivide-by-zero}");33    Console.WriteLine($"testPassed={testPassedTrue}");34}
    outputexpectedException=True
    sawException=True
    outcome=divide-by-zero
    testPassed=True
  1. denominator ← 2, expectedException ← False, sawException ← False

    10static void Main()11{12    int denominator→ 2 = 2;13    bool expectedException→ False = denominator2 == 0;14    bool sawException→ False = false;15    string outcome→ completed = "completed";
  2. try

    17try18{19    int value = Divide(10, denominator2);20    outcome = $"value={value}";
  3. static int Divide(int numerator, int denominator)

    4{5    static int Divide(int numerator10, int denominator2)6    {7        return numerator10 / denominator2;8    }
  4. value ← 5, outcome ← value=5

    18{19    int value→ 5 = Divide(10, denominator2);20    outcome→ value=5 = $"value={value5}";21}
  5. testPassed ← True

    28    bool testPassed→ True = sawExceptionFalse == expectedExceptionFalse;2930    Console.WriteLine($"expectedException={expectedExceptionFalse}");31    Console.WriteLine($"sawException={sawExceptionFalse}");32    Console.WriteLine($"outcome={outcomevalue=5}");33    Console.WriteLine($"testPassed={testPassedTrue}");34}
    outputexpectedException=False
    sawException=False
    outcome=value=5
    testPassed=True
  1. denominator ← 5, expectedException ← False, sawException ← False

    10static void Main()11{12    int denominator→ 5 = 5;13    bool expectedException→ False = denominator5 == 0;14    bool sawException→ False = false;15    string outcome→ completed = "completed";
  2. try

    17try18{19    int value = Divide(10, denominator5);20    outcome = $"value={value}";
  3. static int Divide(int numerator, int denominator)

    4{5    static int Divide(int numerator10, int denominator5)6    {7        return numerator10 / denominator5;8    }
  4. value ← 2, outcome ← value=2

    18{19    int value→ 2 = Divide(10, denominator5);20    outcome→ value=2 = $"value={value2}";21}
  5. testPassed ← True

    28    bool testPassed→ True = sawExceptionFalse == expectedExceptionFalse;2930    Console.WriteLine($"expectedException={expectedExceptionFalse}");31    Console.WriteLine($"sawException={sawExceptionFalse}");32    Console.WriteLine($"outcome={outcomevalue=2}");33    Console.WriteLine($"testPassed={testPassedTrue}");34}
    outputexpectedException=False
    sawException=False
    outcome=value=2
    testPassed=True