NegativeArraySizeException
Codeaft.java
class Codeaft
{
    public static void main(String args[])
    {
        int size=Integer.MIN_VALUE;
        System.out.println(size);
        String[] s=new String[size];
    }
}
Output
codeaft@codeaft:~$ javac Codeaft.java
codeaft@codeaft:~$ java Codeaft -2147483648 Exception in thread "main" java.lang.NegativeArraySizeException: -2147483648 at Codeaft.main(Codeaft.java:7) codeaft@codeaft:~$
Java program to handle the NegativeArraySizeException
Codeaft.java
class Codeaft
{
    public static void main(String args[])
    {
        int size=Integer.MIN_VALUE;
        try
        {
            String[] s=new String[size];
        }
        catch(NegativeArraySizeException e)
        {
            System.out.println("Array underflow");
            System.out.println(e.toString());
        }
    }
}
Output
codeaft@codeaft:~$ javac Codeaft.java
codeaft@codeaft:~$ java Codeaft Array underflow java.lang.NegativeArraySizeException: -2147483648 codeaft@codeaft:~$
Comments and Reactions