Assertion Error:

I guess we all know about assertions,which is one of the great tool to deal with the system failures.

Let us have an overview of how assertions differs from Exception handling technique.We all know about how assertions works.But anyway,Let's have a glance at assertions to know its advantage.

Assertions are used to evaluate a condition.If the condition gets failed then they will throw

java.lang.Assertion error.

Usage of Assertions:

1.assert a!=null;

This statement will check whether 'a' is null or not.If 'a' is null then this statement will throw an Assertion error. you could have thought of throwing an NullPointerException and processing it using the try and catch block rather than using the Assertion error.

But one of the great advantage of assertions when compared to exception handling is that assertions allows us to use condition checks during testing and automatically removes the statements from the generated code thereby not affecting the execution speed.

If you have lot of conditions to be checked in your program and have implemented exception handling techniques for violation of conditions,then this will slower the program execution.

I have shown this slowering of the program execution, using the two programs that i have included in this post.

When do we need this Assertion?

Note that the assertion should be used to deal with unrecoverable errors or errors that will disrupt operation of the entire program.Mostly it will not be used to deal with recoverable conditions.

one of the most common example where assertion can be used is explained through the sorting program.In the sorting program it would be used to check the validity of parameters passed.

If we want to sort an array of elements then before sorting it we should ensure that the array is not null.For this case assertion can be used.If an assertion error occurs then we can terminate the program rather than applying the sorting operations on that array.

The two different implementations of the sorting program that throws assertion error and also an other program which throws NullPointerException is given below.

output 2:

On comparing the output of the above two programs one thing seems to be obvious,which is the time taken to execute the program.

As you see the output of the program1,the time taken for program execution is 0 millisecond,wheareas the second program takes 1 millisecond for program execution.

Though this difference of 1 millisecond will not affect the performance of the program considerably,if you are dealing with some big projects where performance is of utmost importance the execution time does matters.

Suppose if you are in a postion to check many conditions and throw exceptions corresponding to the violation of each conditions then certainly the performance of the program will be affected because even after testing ,the code stays in the program which consumes some execution time.

Thus assertion offers better performance when compared to exception handling,as they remove the code after testing is over.