Blog |

How to Handle the Negative Array Size Exception in Java

How to Handle the Negative Array Size Exception in Java
Table of Contents

The NegativeArraySizeException is a runtime exception in Java that occurs when an application attempts to create an array with a negative size.

Since the NegativeArraySizeException is an unchecked exception, it does not need to be declared in the throws clause of a method or constructor.

 

What Causes NegativeArraySizeException in Java

The NegativeArraySizeException occurs when an attempt is made to assign a negative size to an array. Here's an example:

public class NegativeArraySizeExceptionExample {
    public static void main(String[] args) {
        int[] array = new int[-5];
        System.out.println("Array length: " + array.length);
    }
}

Running the above code throws the following exception:

Exception in thread "main" java.lang.NegativeArraySizeException: -5
    at NegativeArraySizeExceptionExample.main(NegativeArraySizeExceptionExample.java:3)

 

How to Handle NegativeArraySizeException in Java

The NegativeArraySizeException can be handled in code using the following steps:

  • Surround the piece of code that can throw an NegativeArraySizeException in a try-catch block.
  • Catch the NegativeArraySizeException in the catch clause.
  • Take further action as necessary for handling the exception and making sure the program execution does not stop.

Here's an example of how to handle it in code:

public class NegativeArraySizeExceptionExample {
    public static void main(String[] args) {
        try {
            int[] array = new int[-5];
        } catch (NegativeArraySizeException nase) {
            nase.printStackTrace();
            //handle the exception
        }
        System.out.println("Continuing execution...");
    }
}

In the above example, the lines that throw the NegativeArraySizeException are placed within a try-catch block. The NegativeArraySizeException is caught in the catch clause and its stack trace is printed to the console. Any code that comes after the try-catch block continues its execution normally.

Running the above code produces the following output:

java.lang.NegativeArraySizeException: -5
    at NegativeArraySizeExceptionExample.main(NegativeArraySizeExceptionExample.java:4)
Continuing execution...

 

How to Avoid NegativeArraySizeException in Java

Since the NegativeArraySizeException occurs when an array is created with a negative size, assigning a positive size to the array can help avoid the exception. Applying this to the earlier example helps fix the issue:

public class NegativeArraySizeExceptionExample {
    public static void main(String[] args) {
        int[] array = new int[5];
        System.out.println("Array length: " + array.length);
    }
}

The array is initialized with a size of 5, which is a positive number. Running the above code produces the correct output as expected:

Array length: 5

Related Posts

See all posts

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Start continuously improving your code today.

Get Started Shape