<div dir="ltr" style="text-align: justify;" trbidi="on"><b><i>throw </i>keyword:</b><br />
The <i><b>throw </b></i>keyword is used to explicitly throw an exception.<br />
We can throw either checked or <b>unchecked exception. </b>The <i><b>throw </b></i>keyword is mainly used to throw custom exception. We will see custom exceptions later.</p>
<p>In this example, we have created the <i><b>validateStudent() </b></i>method that takes integer value as a parameter. If the age is less than 5, we are throwing the <b><i>ArithmeticException </i></b>otherwise print a message welcome to school.</p>
<pre class="highlight" name="code">class AgeValidator{ 
 
 static void validateStudent(int age){ 
 if(age<;5) 
 throw new ArithmeticException("age to play more"); 
 else 
 System.out.println("welcome to school"); 
 } 
 
 public static void main(String args[]){ 
 validateStudent(4); 
 System.out.println("rest of the code..."); 
 } 
} 
</pre>
<div align='center' id="ads-id"></div>
<p><b>output here:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/throw.png"/></div>
<p>
<b>Difference between throw and throws keywords:</b><br />
<i><b>throw </b></i>is used to actually throw the exception, whereas throws is declarative for the method. They are not interchangeable.</p>
<p> <i><b>Throws</b></i>: this is to be used when you are not using the try catch statement in your code but you know that this particular class is capable of throwing so and so exception(only checked exceptions). In this you do not use try catch block but write using the throw clause at appropriate point in you code and the exception is thrown to caller of the method and is handled by it.</p>
<table border="4" cellpadding="5" cellspacing="2">
<tbody>
<tr>
<td>1)throw is used to explicitly throw an exception.</td>
<td>throws is used to declare an exception.</td>
</tr>
<tr>
<td>2)checked exception can not be propagated without throws.</td>
<td>checked exception can be propagated with throws.</td>
</tr>
<tr>
<td>3)throw is followed by an instance.</td>
<td>throws is followed by class.</td>
</tr>
<tr>
<td>4)throw is used within the method.</td>
<td>throws is used with the method signature.</td>
</tr>
<tr>
<td>5)You cannot throw multiple exception</td>
<td>You can declare multiple exception e.g.<br />
public void method()throws IOException,SQLException.</td>
</tr>
</tbody>
</table>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/the-finally-keyword-finally-keyword-is/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/throws-keyword-in-java/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/the-finally-keyword-finally-keyword-is/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/throws-keyword-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: '499'});
});
</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…