<div dir="ltr" style="text-align: justify;" trbidi="on"><b style="color: #073763; font-size: x-large;">Introduction</b><br />
 In this article we are going to describe the many exceptions that are possibly generated by an array in Java. So now we describe each Exception type in detail one by one. First we give you a little introduction to arrays in Java.</p>
<p> <b><a href="https://dineshonjava.com/array-in-java/">Array Definition</a> ;</b></p>
<p> <a href="https://dineshonjava.com/array-in-java/">An array is a container object that holds a fixed number of values of a single type</a>. The length of an array is established when the array is created. After creation, its length is fixed. And it also contains similar types of data. Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the above illustration, the numbering begins with 0.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/objects-tenElementArray.gif"/></div>
<p>
<b>Possible Error Type</b></p>
<p> <b>1 &#8211; <i>NullPointerException </i>:</b> In Java a <i><b>NullPointerException </b></i>is a class which also extends <b><i>RuntimeException</i></b>. There are the following conditions where the <b><i>NullPointerException </i></b>is generated. They are thrown when an application attempts to use null in a case where an object is required.</p>
<div align='center' id="ads-id"></div>
<p></p>
<ul>
<li>Calling the instance method of a null object.</li>
<li>Accessing or modifying the field of a null object.</li>
<li>Taking the length of null as if it were an array.</li>
<li>Accessing or modifying the slots of null as if it were an array.</li>
<li>Throwing null as if it were a Throwable value.</li>
</ul>
<p>Applications should throw instances of this class to indicate other illegal uses of the null object.</p>
<p> <b>2 &#8211;</b> <i><b>NegativeArraySizeException </b></i>: This error is thrown when anyone wants create an array with a negative size. <b><i>NegativeArraySizeException </i></b>is a class in Java which extends <b><i>RuntimeException</i></b>.</p>
<p> <b>3 &#8211; </b><i><b>ArrayIndexOutOfBoundsException </b></i>: This type of error is generated when an array has been accessed with an illegal index. In other words when an array is accessed by a negative index or more than the size of the array. In Java it&#8217;s a separate class and this class extends the <b><i>IndexOutOfBoundException</i></b> class.</p>
<p> <b>4 &#8211; </b><i><b>IndexOutOfBoundsException </b></i>: This type of exception is thrown by all indexing pattern data types such as an array string and a vector etc., when it is accessed out of the index (range). <b><i>IndexOutOfBoundException </i></b>is also a separate class in Java and it extends <b><i>RuntimeException</i></b>.</p>
<p> <i><b>5 &#8211; ClassCastException :</b></i> The <i><b>ClassCastException </b></i>is thrown when the following code has attempted to cast an object to a subclass of which it is not an instance. <i><b>ClassCastException </b></i>is also a separate class in Java and it extends <i><b>RuntimeException</b></i>.</p>
<p> <i><b>ClassCastException </b></i>condition:</p>
<pre class="highlight" name="code">Object x = new Integer(0); 
System.out.println((String)x); 
</pre>
<p>
<i><b>6 &#8211; ArrayStoreException</b></i> : This type of exception is thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. For example, the following code generates an <i><b>ArrayStoreException</b></i>. <i><b>ArrayStoreException </b></i>is also a separate class in Java and it extends <i><b>RuntimeException</b></i>.</p>
<p> <b>For example:</b></p>
<pre class="highlight" name="code">Object x[] = new String[7]; 
x[0] = new Integer(0); 
</pre>
<p><b>Example:</b></p>
<pre class="highlight" name="code">public class ExceptionInArray { 
 
 public static void main(String[] args) { 
 int array[] = null; 
 int arraySize = 4; 
 int arrayInc = -0; 
 int output; 
 
 for (int i = 0; i <; 6; i++) { 
 try { 
 // Multiple Switch Conditions 
 switch (i) { 
 case 0: 
 output = array[0]; // Generates a NullPointerException. 
 break; 
 case 1: 
 array = new int[arrayInc]; // Generates a 
 // NegativeArraySizeException. 
 output = array[arrayInc]; 
 break; 
 case 2: 
 array = new int[arraySize]; // Generate the 
 // ArrayIndexOutOfBoundsException. 
 output = array[arraySize]; 
 break; 
 case 3: 
 array = new int[5]; // Generate the 
 // IndexOutOfBoundsException. 
 output = array[5]; 
 case 4: 
 Object newArray = new Integer(0); // Generate the 
 // ClassCastException. 
 System.out.println((String) newArray); 
 case 5: 
 Object X[] = new String[-5]; // Generate the 
 // ArrayStoreException. 
 X[0] = new Integer(0); 
 System.out.println(X); 
 } 
 
 } catch (NullPointerException | NegativeArraySizeException | ArrayIndexOutOfBoundsException | ClassCastException | ArrayStoreException ex) { 
 System.out.println("n Exception Generated: " 
 + ex.getMessage()); 
 ex.printStackTrace(); 
 } 
 } 
 } 
} 
</pre>
<p><b>OUTPUT:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" height="409" src="https://dineshonjava.com/wp-content/uploads/2013/04/arrayexception.png" width="600"/></div>
<p><b> ;</b> </p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/multiple-exceptions-in-java-7-new/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/inner-nested-classes-in-java/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/user-defined-exception-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/multiple-exceptions-in-java-7-new/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '494'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…