How to Handle a java.lang.NegativeArraySizeException in Java? Last Updated : 16 Feb, 2024 Summarize Comments Improve Suggest changes Share 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. Comment More infoAdvertise with us Next Article How to Handle SQLException in JDBC? S seepanarajvskq Follow Improve Article Tags : Java Java Programs Java-Exceptions Java Examples Practice Tags : Java Similar Reads How to Handle SQLException in JDBC? Java Database Connectivity (JDBC) serves as the backbone for Java applications when interacting with databases. While establishing connections and executing queries, we developers often encounter SQLExceptions, which are inevitable in the real world. Handling those exceptions is crucial in the devel 4 min read How to Fix a java.lang.StringIndexOutOfBoundsException? In Java, String index out of bound is the common runtime exception that can occur when we try to access or manipulate the string using the invalid index. It can typically happen when the index provides is the negative, equal length of the string or greater than the length of the string. It can be ha 3 min read How to Handle an IOException in Java? An IOException in Java occurs when we try to perform some input or output tasks and then some issues occur. Programmers need to handle this issue explicitly with a piece of code that executes when an issue occurs. There is an entire class for handling such issues known as the IOException class, whic 3 min read How to Handle a java.lang.ArithmeticException in Java? In Java programming, the java.lang.ArithmeticException is an unchecked exception of arithmetic operations. This means you try to divisible by zero, which raises the runtime error. This error can be handled with the ArthmeticException. ArithmeticException can be defined as a runtime exception that ca 2 min read How to handle a java.lang.IndexOutOfBoundsException in Java? In Java programming, IndexOutOfBoundsException is a runtime exception. It may occur when trying to access an index that is out of the bounds of an array. IndexOutOfBoundsException is defined as the RuntimeException. It can be used to find the out-of-bound run-time errors of an array. It is a part of 2 min read How to Solve java.lang.IllegalStateException in Java main Thread? An unexpected, unwanted event that disturbed the normal flow of a program is called Exception. Most of the time exception is caused by our program and these are recoverable. Example: If our program requirement is to read data from the remote file locating in U.S.A. At runtime, if a remote file is no 5 min read Like