
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Can the overriding method throw the super-type of the exception thrown by the overridden method in Java?
If the super-class method throws certain exception, the method in the sub-class should not throw its super type.
Example
In the following example the readFile() method of the super-class throws FileNotFoundException exception and, the readFile() method of the sub-class throws an IOException, which is the super type of the FileNotFoundException.
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super { public String readFile(String path)throws FileNotFoundException { throw new FileNotFoundException(); } } public class ExceptionsExample extends Super { @Override public String readFile(String path)throws IOException { //method body ...... } }
Compile time error
On compiling, the above program gives you the following output −
ExceptionsExample.java:13: error: readFile(String) in ExceptionsExample cannot override readFile(String) in Sup public String readFile(String path)throws IOException { ^ overridden method does not throw IOException 1 error
- Related Articles
- While overriding can the subclass choose not to throw an exception in java?
- Exception handling with method overriding in Java.
- If a method in parent class “throws Exception”, can we remove it in overridden method in java?
- What are the rules of exception handling with respect to method overriding in java?
- Can the abstract methods of an interface throw an exception in java?
- What is the difference between method hiding and method overriding in Java?
- Method overriding in Java
- What are the rules on method overriding in Java?
- What are the rules need to follow when overriding a method that throws an exception in Java?
- What is the purpose of overriding a finalize() method in Java?
- Can we change method signature in overriding in Java?
- Why is method overloading not possible by changing the return type of the method only in java?
- Can a constructor throw an exception in Java?
- Guidelines to follow in while overriding a method that throws an exception in java?
- How to catch an exception thrown by an async void method in C#?

Advertisements