<div dir="ltr" style="text-align: justify;" trbidi="on">The <i><b> throws </b></i>keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained. Exception Handling is mainly used to handle the checked exceptions. If there occurs any unchecked exception such as <b><i> NullPointerException</i></b>, it is programmers fault that he is not performing check up before the code being used.</p>
<p> <b>Syntax of <i>throws </i>keyword:</b></p>
<pre class="highlight" name="code">void method_name() throws exception_class_name{
 ... 
}
</pre>
<p>
<b>Which exception should we declare?</b><br />
<b>checked exception only</b>, because:</p>
<ol>
<li><b>unchecked Exception:</b> under your control so correct your code.</li>
<li><b>error:</b> beyond your control e.g. you are unable to do anything if there occurs <i><b>VirtualMachineError </b></i>or <i><b>StackOverflowError</b></i>.</li>
</ol>
<div align="center" id="ads-id"></div>
<p><i><b>Program which describes that checked exceptions can be propagated by throws keyword.</b></i></p>
<pre class="highlight" name="code">import java.io.IOException;
class Simple{
 void m()throws IOException{
 throw new IOException("device error");//checked exception
 }
 void n()throws IOException{
 m();
 }
 void p(){
 try{
 n();
 }catch(Exception e){System.out.println("exception handled");}
 }
 public static void main(String args[]){
 Simple obj=new Simple();
 obj.p();
 System.out.println("normal flow...");
 }
}
</pre>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
exception handled<br />
normal flow&#8230;</div>
<p><b><br />
</b> <b><i>Rule</i>: If you are calling a method that declares an exception, you must either caught or declare the exception.</b></p>
<p><b>There are two cases: </b></p>
<ol>
<li><b>Case1:</b>You caught the exception i.e. handle the exception using <b><i>try/catch</i></b>.</li>
<li><b>Case2:</b>You declare the exception i.e. specifying <i><b>throws </b></i>with the method.</li>
</ol>
<p>
<b>Case1: You handle the exception</b><br />
In case you handle the exception, the code will be executed fine whether exception occurs during the program or not.</p>
<pre class="highlight" name="code">import java.io.*;
class M{
 void method()throws IOException{
 throw new IOException("device error");
 }
}


class Test{
 public static void main(String args[]){
 try{
 Test t=new Test();
 t.method();
 }catch(Exception e){System.out.println("exception handled");} 

 System.out.println("normal flow...");
 }
}
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
exception handled<br />
normal flow&#8230;</div>
<p>
<b>Case 2: You declare the exception</b></p>
<ul>
<li>In case you declare the exception, if exception does not occur, the code will be executed fine.</li>
<li>In case you declare the exception if exception occurs, an exception will be thrown at runtime because throws does not handle the exception.</li>
</ul>
<p> ;<b><i>A)Program if exception does not occur</i></b></p>
<pre class="highlight" name="code">import java.io.*;
class M{
 void method()throws IOException{
 System.out.println("device operation performed");
 }
}


class Test{
 public static void main(String args[])throws IOException{//declare exception
 Test t=new Test();
 t.method(); 

 System.out.println("normal flow...");
 }
}
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
device operation performed<br />
normal flow&#8230;</div>
<p>
<b><i>B)Program if exception occurs</i></b></p>
<pre class="highlight" name="code">import java.io.*;
class M{
 void method()throws IOException{
 throw new IOException("device error");
 }
}


class Test{
 public static void main(String args[])throws IOException{//declare exception
 Test t=new Test();
 t.method(); 

 System.out.println("normal flow...");
 }
}
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
Runtime Exception</div>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/2013/04/difference-between-throw-and-throws-in.html">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html">Index </a>||  ; >;>;<a href="https://dineshonjava.com/2013/04/exception-propagation-in-java.html">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/difference-between-throw-and-throws-in/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/exception-propagation-in-java/">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: '498'});
});
</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…