- 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
What are the rules need to follow when overriding a method that throws an exception in Java?
We need to follow some rules when we overriding a method that throws an Exception.
- When the parent class method doesn’t throw any exceptions, the child class method can’t throw any checked exception, but it may throw any unchecked exceptions.
class Parent { void doSomething() { // ... } } class Child extends Parent { void doSomething() throws IllegalArgumentException { // ... } }
- When the parent class method throws one or more checked exceptions, the child class method can throw any unchecked exception.
class Parent { void doSomething() throws IOException, ParseException { // ... } void doSomethingElse() throws IOException { // ... } } class Child extends Parent { void doSomething() throws IOException { // ... } void doSomethingElse() throws FileNotFoundException, EOFException { // ... } }
- When the parent class method has a throws clause with an unchecked exception, the child class method can throw none or any number of unchecked exceptions, even though they are not related.
class Parent { void doSomething() throws IllegalArgumentException { // ... } } class Child extends Parent { void doSomething() throws ArithmeticException, BufferOverflowException { // ... } }
Example
import java.io.*; class SuperClassTest{ public void test() throws IOException { System.out.println("SuperClassTest.test() method"); } } class SubClassTest extends SuperClassTest { public void test() { System.out.println("SubClassTest.test() method"); } } public class OverridingExceptionTest { public static void main(String[] args) { SuperClassTest sct = new SubClassTest(); try { sct.test(); } catch(IOException ioe) { ioe.printStackTrace(); } } }
Output
SubClassTest.test() method
- Related Articles
- Guidelines to follow in while overriding a method that throws an exception in java?
- What are the rules of exception handling with respect to method overriding in java?
- What are the rules on method overriding in Java?
- What are the rules we need to follow in JShell in Java 9?
- Rules for Java method overriding
- Can we change an exception of a method with throws clause from unchecked to checked while overriding it in java?
- Exception handling with method overriding in Java.
- What are the rules to follow while using exceptions in java lambda expressions?
- When overriding clone method, why do we need to declare it as public in Java?
- When Method Overriding occurs in Java?
- What are some of the unwritten social rules that everybody should follow?
- What are the Rules of Netiquette You Must Follow?
- How do you test that a Python function throws an exception?
- While overriding can the subclass choose not to throw an exception in java?
- Is it possible to throw exception without using "throws Exception" in java?

Advertisements