<div dir="ltr" style="text-align: justify;" trbidi="on"><b>Exceptions in Java &#8211; What are Java Exceptions?</b><br />
A Java Exception is an abnormal condition that arises during the execution of a program and also called a run-time error. An exception in Java signals the unusual or catastrophic situations that can arise.<br />
<b>Examples of Java Exceptions are:</b></p>
<ul>
<li>Incorrect user input such as division by zero</li>
<li>File that needs to be opened not found</li>
<li>Network connection problem</li>
</ul>
<p><b> Types of Java Exceptions</b><br />
Like anything else in this world, Exceptions can also be categorized! Based on whether or not you are required to handle the exception, there are two types of exceptions:</p>
<ol>
<li>Checked Exceptions</li>
<li>Unchecked Exceptions.</li>
</ol>
<p><b>Checked Exceptions: </b>These are the type of exceptions for which the compiler checks to ensure that your code is prepared for handling such exceptions. These are the exceptions that you can expect to occur frequently and you must handle them in your code. The conditions that generate such exceptions are generally outside the control of your program and they can occur in a correct program. But you can anticipate them and thus you must write code to deal with them. Programmatically, checked exceptions are the instances of the Exception class or one of its subclasses, excluding <i><b>RuntimeException </b></i>subtree. </p>
<div align='center' id="ads-id"></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffb62a; color: black; font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; margin: 0px; orphans: auto; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;"><b>List of checked exception</b></div>
<ul style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffb62a; color: black; font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<li><i><b>ClassNotFoundException </b></i>Class not found.</li>
<li><i><b>CloneNotSupportedException </b></i>Attempt to clone an object that does not implement the<u>Cloneable</u> ;interface.</li>
<li><i><b>IllegalAccessException </b></i>Access to a class is denied.</li>
<li><i><b>InstantiationException </b></i>Attempt to create an object of an abstract class or interface.</li>
<li><i><b>InterruptedException </b></i>One thread has been interrupted by another thread.</li>
<li><i><b>NoSuchFieldException </b></i>A requested field does not exist.</li>
<li><i><b>NoSuchMethodException </b></i>A requested method does not exist.</li>
</ul>
<p><b>Unchecked Exceptions: </b>The compiler does not check for such type of exceptions. Unchecked Exceptions comprise of run time exceptions (of type <i><b>RuntimeException </b></i>or its subclasses) and errors (of type Error or its subclasses). Runtime Exceptions occur due to program bugs and include exceptions such as division by zero and invalid array indexing. You can not recover from an unchecked exception and you are not required to handle such type of exceptions either, but still you can do so if you want. <b><i>ArithmeticException </i></b>class is an example of unchecked exception. Errors are the exceptions that are not expected to be caught under normal circumstances by your program. They are used by the Java run time environment to indicate errors related to run time environment itself. Stack Overflow is an example of error.</p>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffb62a; color: black; font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; margin: 0px; orphans: auto; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;"><b>List of unchecked exception</b></div>
<ul style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffb62a; color: black; font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<li><i><b>ArithmeticException </b></i>Arithmetic error, such as divide-by-zero.</li>
<li><i><b>ArrayIndexOutOfBoundsException </b></i>Array index is out-of-bounds.</li>
<li><i><b>ArrayStoreException </b></i>Assignment to an array element of an incompatible type.</li>
<li><i><b>ClassCastException </b></i>Invalid cast.</li>
<li><b><i>IllegalArgumentException </i></b>Illegal argument used to invoke a method.</li>
<li><i><b>IllegalMonitorStateException </b></i>Illegal monitor operation, such as waiting on an unlocked thread.</li>
<li><i><b>IllegalStateException </b></i>Environment or application is in incorrect state.</li>
<li><i><b>IllegalThreadStateException </b></i>Requested operation not compatible with current thread state.</li>
<li><i><b>IndexOutOfBoundsException </b></i>Some type of index is out-of-bounds.</li>
<li><i><b>NegativeArraySizeException </b></i>Array created with a negative size.</li>
<li><i><b>NullPointerException </b></i>Invalid use of a null reference.</li>
<li><i><b>NumberFormatException </b></i>Invalid conversion of a string to a numeric format.</li>
<li><i><b>SecurityException </b></i>Attempt to violate security.</li>
<li><i><b>StringIndexOutOfBounds </b></i>Attempt to index outside the bounds of a string.</li>
<li><i><b>UnsupportedOperationException </b></i>An unsupported operation was encountered.</li>
</ul>
<p></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/Exception.png"/></div>
<p><b>Common scenarios of Exception Handling where exceptions may occur</b><br />
There are given some scenarios where unchecked exceptions can occur. They are as follows:<br />
<b>1) Scenario where <i>ArithmeticException </i>occurs</b><br />
If we divide any number by zero, there occurs an <i><b>ArithmeticException</b></i>.</p>
<pre class="highlight" name="code">int a=50/0;//ArithmeticException 
</pre>
<p><b><br />
2) Scenario where <i>NullPointerException </i>occurs</b><br />
If we have null value in any variable, performing any operation by the variable occurs an <b><i>NullPointerException</i></b>.</p>
<pre class="highlight" name="code">String s=null; 
System.out.println(s.length());//NullPointerException 
</pre>
<p><b>3) Scenario where <i>NumberFormatException </i>occurs</b><br />
The wrong formatting of any value, may occur <i><b>NumberFormatException</b></i>. Suppose I have a string variable that have characters, converting this variable into digit will occur <b><i>NumberFormatException</i></b>.</p>
<pre class="highlight" name="code">String s="abc"; 
int i=Integer.parseInt(s);//NumberFormatException 
</pre>
<p><b>4) Scenario where <i>ArrayIndexOutOfBoundsException </i>occurs</b><br />
If you are inserting any value in the wrong index, it would result <i><b>ArrayIndexOutOfBoundsException </b></i>as shown below:</p>
<pre class="highlight" name="code">int a[]=new int[5]; 
a[10]=50; //ArrayIndexOutOfBoundsException 
</pre>
<p><b><br />
</b> <b>Exceptions Methods:</b><br />
Following is the list of important methods available in the <b>Throwable </b>class.</p>
<table class="src" style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f7f7f7; border-collapse: collapse; border: 1px solid rgb(214, 214, 214); color: black; font-family: Helvetica, Arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; margin: 8px 0px; orphans: auto; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; vertical-align: top; white-space: normal; widows: auto; width: 560px; word-spacing: 0px;">
<tbody style="margin: 0px; padding: 0px;">
<tr style="margin: 0px; padding: 0px;">
<th style="background-color: #eeeeee; border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px; text-align: left;">SN</th>
<th style="background-color: #eeeeee; border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px; text-align: left;">Methods with Description</th>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">1</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">public String getMessage()</b><br />
Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">2</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">public Throwable getCause()</b><br />
Returns the cause of the exception as represented by a Throwable object.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">3</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">public String toString()</b><br />
Returns the name of the class concatenated with the result of getMessage()</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">4</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">public void printStackTrace()</b><br />
Prints the result of toString() along with the stack trace to System.err, the error output stream.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">5</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">public StackTraceElement [] getStackTrace()</b><br />
Returns an array containing each element on the stack trace. The element at index 0 represents the top of the call stack, and the last element in the array represents the method at the bottom of the call stack.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">6</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">public Throwable fillInStackTrace()</b><br />
Fills the stack trace of this Throwable object with the current stack trace, adding to any previous information in the stack trace.</td>
</tr>
</tbody>
</table>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/instance-initializer-block-in-java/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/try-catch-block-and-handling-exceptions/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/instance-initializer-block-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/try-catch-block-and-handling-exceptions/">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: '504'});
});
</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…