<div dir="ltr" style="text-align: justify;" trbidi="on">You can also create your own exception sub class simply by extending java <i><b>Exception </b></i>class. You can define a constructor for your Exception sub class (not compulsory) and you can override the <b><i>toString()</i></b> function to display your customized message on catch.</p>
<pre class="highlight" name="code">class InvalidAgeException extends Exception{ 
 InvalidAgeException(String s){ 
 super(s); 
 } 
 public String toString() 
 { 
 return "Candidate is less than 18 year is not allowed to vote."; 
 } 
} 
</pre>
<div align='center' id="ads-id"></div>
<pre class="highlight" name="code">class ValidateCandidate{ 
 
 static void validate(int age) throws InvalidAgeException{ 
 if(age <; 18) 
 throw new InvalidAgeException("invalid candidate"); 
 else 
 System.out.println("welcome to vote"); 
 } 
 
 public static void main(String args[]){ 
 try{ 
 validate(13); 
 }catch(Exception ex){ 
 System.out.println("Exception occured: "+ex); 
 } 
 
 System.out.println("rest of the code..."); 
 } 
} 
</pre>
<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/userdefine.png"/></div>
<p><b>Points to Remember</b></p>
<p></p>
<ol>
<li>Extend the Exception class to create your own <i><b>exception </b></i>class.</li>
<li>You don&#8217;t have to implement anything inside it, no methods are required.</li>
<li>You can have a Constructor if you want.</li>
<li>You can override the <i><b>toString()</b></i> function, to display customized message.</li>
</ol>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/handle-exceptions-in-overriding-methods/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/multiple-exceptions-in-java-7-new/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/handle-exceptions-in-overriding-methods/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/different-exception-generate-in-array/">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: '495'});
});
</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…