How to Handle a java.lang.NegativeArraySizeException in Java? Last Updated : 16 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In Java, NegativeArraySizeException is the pre-defined class that can be used to represent the unchecked exception. It is a subclass of the java.lang package, and it can be used to identify that an array was attempted to be created with a negative size, which is not allowed in Java. In this article, we will learn how to handle a java.lang.NegativeArraySizeException in Java. Step-by-step Implementation:Create the class named GfGNegativeArraySize and write the main method into the class.Create the try block then write the logic into that block.Create one variable named size and assign the value of -5.Now, initialize the array with the size of the previously defined.Write the catch block for NegativeArraySizeException with print with error statement.Run the program if identifies the array created with a negative size then print the error message.Program to Handle a java.lang.NegativeArraySizeException in Java Java // Java Program to handle a NegativeArraySizeException import java.lang.Exception; import java.lang.NegativeArraySizeException; // Driver Class public class GfGNegativeArraySize { // Main Function public static void main(String args[]) { try { int size = -5; // Attempting to create an array // With a negative size int[] array = new int[size]; } catch (NegativeArraySizeException e) { // Handle the NegativeArraySizeException System.out.println("Error: Attempted to create an array with a negative size."); // Can add more detailed error handling or logging here if needed } } } OutputError: Attempted to create an array with a negative size. Explanation of the Program:We have created an array with a negative size.The attempt to create such an array throws a NegativeArraySizeException.Catch this exception using a try-catch block.In the catch block, it handles the exception by printing an error message indicating that an attempt was made to create an array with a negative size.We can extend the error handling or logging within the catch block as needed for our application. Create Quiz Comment S seepanarajvskq Follow 0 Improve S seepanarajvskq Follow 0 Improve Article Tags : Java Java Programs Java-Exceptions Java Examples Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface4 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like