Add to Google Reader or Homepage

java.lang.AssertionError


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.

program 1:
------------

 class Desc1
{
public void sort(int[] a)
{
int t;
for(int j=0;j<=5;j++)
{
for(int i=j+1;i<5;i++)
{
if(a[j]
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(int h=0;h<5;h++)
System.out.println(a[h]);
}
}
class Desc
{
public static void main(String args[])
{
long start=System.currentTimeMillis();
System.out.println("start time:"+start);
int a[]={2,7,4,5,6};
a=null;
try
{
assert a!=null:a;
Desc1 d=new Desc1();
d.sort(a);
}

catch(AssertionError e)
{
long end=System.currentTimeMillis();
System.out.println("End time:"+end);
System.out.println("Time elapsed:"+(end-start));
}
}
}


output 1:



C:\blog>javac Desc.java

C:\blog>java -ea Desc
start time:1341553937959
End time:1341553937959
Time elapsed:0




program 2:
----------



 class Desc1
{
public void sort(int a[])
{
int t;
for(int j=0;j<=5;j++)
{
for(int i=j+1;i<5;i++)
{
if(a[j]
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(int h=0;h<5;h++)
System.out.println(a[h]);
}
}
class Desc
{
public static void main(String args[])
{
long start=System.currentTimeMillis();
System.out.println("start time:"+start);
int a[]={2,7,4,5,6};
a=null;
try
{
if(a==null)
throw new NullPointerException();
Desc1 d=new Desc1();
d.sort(a);
}
catch(NullPointerException e)
{
long end=System.currentTimeMillis();
System.out.println("End time:"+end);
System.out.println("Time elapsed:"+(end-start));
}
}
}

output 2:


C:\blog>javac Desc.java

C:\blog>java -ea Desc
start time:1341554333676
End time:1341554333677
Time elapsed:1

C:\blog>



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.


 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top