- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
While overriding can the subclass choose not to throw an exception in java?
If the super-class method throws certain exception, you can override it without throwing any exception.
Example
In the following example the sampleMethod() method of the super-class throws FileNotFoundException exception and, the sampleMethod() method does not throw any exception at all. Still this program gets compiled and executed without any errors.
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super { public void sampleMethod()throws FileNotFoundException { System.out.println("Method of superclass"); } } public class ExceptionsExample extends Super { public void sampleMethod() { System.out.println("Method of Subclass"); } public static void main(String args[]) { ExceptionsExample obj = new ExceptionsExample(); obj.sampleMethod(); } }
Output
Method of Subclass
- Related Articles
- While chaining, can we throw unchecked exception from a checked exception in java?
- Can a constructor throw an exception in Java?
- Guidelines to follow in while overriding a method that throws an exception in java?
- Can the overriding method throw the super-type of the exception thrown by the overridden method in Java?
- Can the abstract methods of an interface throw an exception in java?
- Can we change an exception of a method with throws clause from unchecked to checked while overriding it in java?
- Can we throw an Unchecked Exception from a static block in java?
- Exception handling with method overriding in Java.
- \nHow to throw an exception from a static block in Java? \n
- Is it possible to throw exception without using "throws Exception" in java?
- What are the rules need to follow when overriding a method that throws an exception in Java?
- How do you throw an Exception without breaking a for loop in java?
- How can I get a JavaScript stack trace when I throw an exception?
- Throw Custom Exception in Kotlin
- How to throw custom exception in Kotlin?

Advertisements