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(); } 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(); } An assertion is roughly equivalent to:
if (!condition) { throw new AssertionError(); } -ea switch is passed to the JVMReplacing these assertions would take the form of the following validation:
if (rodzic == null) throw new MyValidationException("rodzic cannot be null"); if (rodzic.rodzic == null) throw new MyValidationException("rodzic.rodzic cannot be null"); return rodzic.getBrat(); Note that there's a distinction between throwing an Exception and an Error - Exceptions are meant to be caught and handled farther up, while Errors indicate a situation that you can't recover from. For example, you might consider a defining and using a MyValidationError if the failed check is irrecoverable.