javaassertion

How to replace assert?


How can I replace assertions with ifs? Example:

public Wezel<Wartosc,Indeks> getWujek() 
{
    assert rodzic != null; // Root node has no uncle
    assert rodzic.rodzic != null; // Children of root has no uncle
    return rodzic.getBrat();
}

Solution

  • An assertion is roughly equivalent to:

    if (!condition) {
        throw new AssertionError();
    }