Documenting and Checking Assumptions

<aside> 💡 Functions may have constraints on its parameters.

</aside>


Assertions

<aside> 💡 If assertions fails, code throws AssertionError

</aside>

public static double myfunc(double x){
	assert x >= 0 : x;
}

<aside> 💡 This exception SHOULD NOT be CAUGHT.

</aside>


Enabling and Disabling Assertions

<aside> 💡 Assertions are enabled or disabled at runtime.

</aside>

Can also use -ea as abbreviation for -enableassertions/tu

Can selectively turn on assertions for a class: java -ea:Myclass MyCode

Can selectively turn on assertions for a package: java -ea:in.ac.iitm.onlinedegree MyCode

Also, Disable assertions globally or selectively: java -disableassertions MyCode

java -da:Myclass MyCode

Can combine as:

java -ea in.ac.iitm.onlinedegree -da:Myclass MyCode

Separate switch to enable assertions for system classes: java -enablesystemassertions MyCode

or java -esa MyCode