Add to Google Reader or Homepage

Handling Java.lang.NegativeArraySizeException

NegativeArraySizeException

The Negative Array size exception is one of the rarely occurring exception in java programming. Let's have a brief overlook on this exception.

If you  glimpse at the title,may be you would get an hint that this exception is related to size of the array.Yes,of course.It is related to the size of the array.

We have studied that,when defining an array in java(and almost in all the programming languages ) we should give a positive value for the size of the array.As we have read we used to give only positive values.

But,I guess we may not know what will happen if we give negative value to the size of the array?..

Even if you specify a negative value,the compiler will accept it and it will not show any error.The fact is that the compiler is designed to verify whether the value given for the size is an integer or not.

It will not check whether the given value is positive or not at the compile time and throws this Negative Array Size Exception.

We programmers,will not give negative value for an array size intentionally.But may be responsible for assigning a negative value.

There are certain situations in which the value for the size of an array goes negative.For example, You could have assigned the result of  a mathematical calculation to be the size value.If the mathematical calculation results in a negative value then you would be landed in a embarrassing situation.

So I suggest you to assert the result of an computation or a value read from a stream to be positive before assigning it as a size of the array.

Here i have included an example program that gives you an idea about when this exception is thrown.

Program:

import java.util.*;
import java.io.*;
public class Stacktest
{
public static void main(String args[])throws IOException
{
int c[]=new int[-2];
Scanner in=new Scanner(new InputStreamReader(System.in));
int b=in.nextInt();
int a[]=new int[b];
}
}


output:

-2
Exception in thread "main" java.lang.NegativeArraySizeException
        at Stacktest.main(Stacktest.java:10)


In the above program i have given the value for the size the array c[] as -2,Which is the reason for this exception to be thrown.

4 comments:

Wes Bartell said...

This is great is there any way you could post an example of code on how to fix it?

Naviya Nair said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions








Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Java Course in Chennai

Divit said...

Thank you for using my Guide and if it work for you that makes me happy



Spark Training in Chennai

Debokti Ghosh said...


/**
* Write a description of class ExeptionProg3 here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
public class neg_arr_size_exep
{


public static void main(String args[]){

Scanner sc=new Scanner(System.in);
try{
int[] arr=new int[-2];
for(int i=0;i<arr.length;i++)
{
System.out.println("enter "+i+" th element");
arr[i]=sc.nextInt();
}

for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
catch(NegativeArraySizeException e)
{System.out.println("caught and thrown");}
}}

Post a Comment

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