Employee employee = null;
//...
//Get an Employee object
//...
//Ensure we have one
assert emplyee!= null;
This asserts that an employee is not null. If employee is null, an AssertionError is thrown. Any line of code executing after the assert statement can safely assume that employee is not null.
Each assertion is a boolean statement when an assertion executes,it returns true and when it returns false then an error is thrown.So successful execution of an assumption will ensure an error free code.By including assertions in one's program,the following objectives can be achieved:
-The quickest way of identifying and correcting bugs.
-The code becomes less prone to errors hence better maintained against errors and more efficient.
-Better readability of code.


1 comments:
Assertion is only used in debug and testing phase, not for deployment because it can be enabled/disabled during compilation
Post a Comment