<div dir="ltr" style="text-align: justify;" trbidi="on">There are few things to remember when overriding a method with exception handling. If super class method does not declare any exception, then sub class overridden method cannot declare checked exception but it can declare unchecked exceptions.</p>
<p><b>Example of Subclass overridden Method declaring Checked Exception</b></p>
<pre class="highlight" name="code">import java.io.*; 
class Super 
{ 
 void show() { 
 System.out.println("parent class"); 
 } 
} 
 
public class Sub extends Super 
{ 
 void show() throws IOException //Compile time error 
 { 
 System.out.println("parent class"); 
 } 
 
 public static void main( String[] args ) 
 { 
 Super s=new Sub(); 
 s.show(); 
 } 
} 
</pre>
<div align="center" id="ads-id"></div>
<p>As the method show() doesn&#8217;t throws any exception while in Super class, hence its overriden version can also not throw any checked exception.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" height="149" src="https://dineshonjava.com/wp-content/uploads/2013/04/sub.png" width="600" /></div>
<p>
<b>Example of Subclass overriden Method declaring Unchecked Exception:</b></p>
<pre class="highlight" name="code">import java.io.*; 
class Super 
{ 
 void show(){ 
 System.out.println("parent class"); 
 } 
} 
 
public class Sub extends Super 
{ 
 void show() throws ArrayIndexOutOfBoundsException //Correct 
 { 
 System.out.println("child class"); 
 } 
 
 public static void main(String[] args) 
 { 
 Super s=new Sub(); 
 s.show(); 
 } 
} 
</pre>
<p>
Because <i><b>ArrayIndexOutOfBoundsException </b></i>is an unchecked exception hence, overridden show() method can throw it.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/sub1.png" /></div>
<p>
<b>More about Overridden Methods and Exceptions:</b></p>
<p>If Super class method throws an exception, then Subclass overridden method can throw the same exception or no exception, but must not throw parent exception of the exception thrown by Super class method.</p>
<p> It means, if Super class method throws object of <b><i>NullPointerException </i></b>class, then Subclass method can either throw same exception, or can throw no exception, but it can never throw object of Exception class (parent of <i><b>NullPointerException </b></i>class).</p>
<p><i><b>Example of Subclass overridden method with same Exception</b></i></p>
<pre class="highlight" name="code">import java.io.*; 
class Super 
{ 
 void show() throws Exception 
 { 
 System.out.println("parent class"); 
 } 
} 
 
public class Sub extends Super { 
 void show() throws Exception //Correct 
 { 
 System.out.println("child class"); 
 } 
 
 public static void main(String[] args) 
 { 
 try { 
 Super s=new Sub(); 
 s.show(); 
 } 
 catch(Exception e){} 
 } 
} 
</pre>
<p></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/sub1.png" /></div>
<p>
<b><i>Example of Subclass overriden method with no Exception:</i></b></p>
<pre class="highlight" name="code">import java.io.*; 
class Super 
{ 
 void show() throws Exception 
 { 
 System.out.println("parent class"); 
 } 
} 
 
public class Sub extends Super { 
 void show() //Correct 
 { 
 System.out.println("child class"); 
 } 
 
 public static void main(String[] args) 
 { 
 try { 
 Super s=new Sub(); 
 s.show(); 
 } 
 catch(Exception e){} 
 } 
} 
</pre>
<p></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/sub1.png" /></div>
<p>
<i><b>Example of Subclass overriden method with parent Exception:</b></i></p>
<pre class="highlight" name="code">import java.io.*; 
class Super 
{ 
 void show() throws ArithmeticException 
 { 
 System.out.println("parent class"); 
 } 
} 
 
public class Sub extends Super { 
 void show() throws Exception //Cmpile time Error 
 { 
 System.out.println("child class"); 
 } 
 
 public static void main(String[] args) 
 { 
 try { 
 Super s=new Sub(); 
 s.show(); 
 } 
 catch(Exception e){} 
 } 
} 
</pre>
<p></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" height="101" src="https://dineshonjava.com/wp-content/uploads/2013/04/sub2.png" width="600" /></div>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/exception-propagation-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/user-defined-exception-in-java/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/exception-propagation-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/user-defined-exception-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: '496'});
});
</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…