<div dir="ltr" style="text-align: justify;" trbidi="on">A method catches an exception using a combination of the try and catch keywords. A <b><i>try/catch </i></b>block is placed around the code that might generate an exception. Code within a <b><i>try/catch</i></b> block is referred to as protected code, and the syntax for using <i><b>try/catch</b></i> looks like the following:</p>
<pre class="highlight" name="code">try 
{ 
 //Protected code 
}catch(ExceptionName e1) 
{ 
 //Catch block 
} 
</pre>
<p><b><br />
Exception Handling in Java</b></p>
<p> It is a powerful feature provided in Java. It is a mechanism to handle run-time errors, so that the normal flow of the program is maintained.</p>
<p> Exception handling consists of the following five keywords:</p>
<p></p>
<div align="center" id="ads-id"></div>
<ol>
<li>Try</li>
<li>Catch</li>
<li>Finally</li>
<li>Throw</li>
<li>Throws</li>
</ol>
<p><b>try block</b><br />
Enclose the code that might throw an exception in <b><i>try </i></b>block. It must be used within the method and must be followed by either <i><b>catch </b></i>or <b><i>finally </i></b>block.<br />
<b>Syntax of <i>try </i>with <i>catch </i>block</b></p>
<pre class="highlight" name="code">try{ 
... 
}catch(Exception_class_Name reference){} 
</pre>
<p><b><br />
Syntax of <i>try </i>with <i>finally </i>block</b></p>
<pre class="highlight" name="code">try{ 
... 
}finally{} 
</pre>
<p><b>catch block</b><br />
<i><b>Catch </b></i>block is used to handle the Exception. It must be used after the <b><i>try </i></b>block.<br />
<b><br />
Code without exception handling:</b></p>
<pre class="highlight" name="code">class App{ 
 public static void main(String args[]){ 
 int index = 10/0; 
 
 System.out.println("another code here"); 
 } 
 } 
</pre>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/error1.png"/></div>
<p>As displayed in the above example, rest of the code is not executed i.e. <b><i>another code here</i></b> statement is not printed. Let&#8217;s see what happens behind the scene:</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" height="462" src="https://dineshonjava.com/wp-content/uploads/2013/04/exceptionobject.jpg" width="600"/></div>
<p>The <a href="https://dineshonjava.com/java-virtual-machine/"><b>JVM </b></a>firstly checks whether the exception is handled or not. If exception is not handled, <a href="https://dineshonjava.com/java-virtual-machine/"><b>JVM</b> </a>provides a default exception handler that performs the following tasks:</p>
<ul>
<li>Prints out exception description.</li>
<li>Prints the stack trace (Hierarchy of methods where the exception occurred).</li>
<li>Causes the program to terminate.</li>
</ul>
<p>
 But if exception is handled by the application programmer, normal flow of the application is maintained i.e. rest of the code is executed. ;<br />
<b>Code with exception handling:</b> </p>
<pre class="highlight" name="code">class App{ 
 public static void main(String args[]){ 
 try{ 
 int index = 10/0; 
 }catch(ArithmeticException e){ 
 System.out.println(e); 
 } 
 System.out.println("another code here"); 
 } 
 } 
</pre>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/error2.png"/></div>
<p>Now, as displayed in the above example, rest of the code is executed i.e. <b><i>another code here</i></b>. statement is printed. </p>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/exception-handling-and-checked-and/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/multiple-catch-block-handling/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/exception-handling-and-checked-and/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/multiple-catch-block-handling/">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: '503'});
});
</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…