BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Exception handling in Method overriding with example

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In the last post we discussed about method overriding. In this post we will see how to do exception handling for overriding and overridden methods.

Rule:  An overriding method (the method of child class) can throw any unchecked exceptions, regardless of whether the overridden method (method of base class) throws exceptions or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw those checked exceptions, which have less scope than the exception(s) declared in the overridden method.

Let’s understand the above explanation with the help of few examples:

Example 1: If base class doesn’t throw any exception but child class throws an unchecked exception.
In this example class Room is overriding the method color(). The overridden method is not throwing any exception however the overriding method is throwing an unchecked exception (NullPointerException). Upon compilation code ran successfully.

class Building {  
   void color()
   {
       System.out.println("Blue");
   }  
}
class Room extends Building{
   //It throws an unchecked exception
   void color() throws NullPointerException
   {
       System.out.println("White");
   }  
   public static void main(String args[]){  
       Building obj = new Room();  
       obj.color(); 
   } 
}

Output:

White

Example 2: If base class doesn’t throw any exception but child class throws an checked exception

import java.io.*;
class Building {  
   void color()
   {
      System.out.println("Blue");
   }  
}
class Room extends Building{
   void color() throws IOException
   {
      System.out.println("White");
   }  
   public static void main(String args[]){  
      Building obj = new Room();  
      try{
         obj.color();
      }catch(Exception e){
         System.out.println(e);
       }
   } 
}

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Exception IOException is not compatible with throws clause in Building.color()

The above code is having a compilation error: Because the overriding method (child class method) cannot throw a checked exception if the overridden method(method of base class) is not throwing an exception.

Example 3: When base class and child class both throws a checked exception

import java.io.*;
class Building {  
   void color() throws IOException
   {
       System.out.println("Blue");
   }  
}
class Room extends Building{
    void color() throws IOException
    {
        System.out.println("White");
    }  
    public static void main(String args[]){  
        Building obj = new Room();  
        try{
	   obj.color();
	}catch(Exception e){
	   System.out.println(e);
	 }
    } 
}

Output:

White

The code ran fine because color() method of child class is NOT throwing a checked exception with scope broader than the exception declared by color() method of base class.

Example 4: When child class method is throwing border checked exception compared to the same method of base class

package beginnersbook.com;
import java.io.*;
class Building {  
	void color() throws IOException
	{
		  System.out.println("Blue");
	}  
}
class Room extends Building{
	  void color() throws Exception
	  {
		  System.out.println("White");
	  }  
	  public static void main(String args[]){  
		   Building obj = new Room();  
		   try{
		   obj.color();
		   }catch(Exception e){
			   System.out.println(e);
		   }
	  } 
}

Output:
Compilation error because the color() method of child class is throwing Exception which has a broader scope than the exception thrown by method color() of parent class.

Top Related Articles:

  1. How to write to file in Java using BufferedWriter
  2. Java Exception Handling Examples
  3. TreeMap in Java with Example
  4. Final Keyword In Java – Final variable, Method and Class
  5. Constructor Overloading in Java with examples

Tags: Java-OOPs

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Vanessa Silva says

    November 3, 2014 at 8:31 PM

    Excellent article, I found it very usefull :D

    Reply
  2. satyanarayana.lella says

    April 4, 2016 at 11:17 AM

    Excellent. Thanks for providing some valuable examples also.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap