- 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
Can we change an exception of a method with throws clause from unchecked to checked while overriding it in java?
A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions.
An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.
Unchecked to checked
When a method in superclass throws an unchecked exception the method the subclass method overriding cannot throw a checked exception.
Example
In the following example we have an abstract method in the class named Super which throws an unchecked exception (ArithmeticException).
In the subclass we are overriding this method and throwing a checked exception (IOException).
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; import java.io.InvalidClassException; abstract class Super { public abstract String readFile(String path) throws ArithmeticException; } public class ExceptionsExample extends Super { @Override public String readFile(String path) throws FileNotFoundException { // TODO Auto-generated method stub return null; } }
Output
ExceptionsExample.java:12: error: readFile(String) in ExceptionsExample cannot override readFile(String) in Super public String readFile(String path) throws FileNotFoundException { ^ overridden method does not throw FileNotFoundException 1 error
But, when a method in superclass throws a checked exception the method the subclass method overriding can throw an unchecked exception.
Example
In the following example we are Just swapping the exceptions of the methods in superclass and subclass.
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; import java.io.InvalidClassException; abstract class Super{ public abstract String readFile(String path) throws FileNotFoundException ; } public class ExceptionsExample extends Super { @Override public String readFile(String path) throws ArithmeticException { // TODO Auto-generated method stub return null; } }
If you compile the above program, it gets compiled without compile time errors
Output
Error: Main method not found in class ExceptionsExample, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
- Related Articles
- While chaining, can we throw unchecked exception from a checked exception in java?
- Guidelines to follow in while overriding a method that throws an exception in java?
- Can we throw an Unchecked Exception from a static block in java?
- How can we decide that custom exception should be checked or unchecked in java?
- Difference Between Checked and Unchecked Exception in Java
- If a method in parent class “throws Exception”, can we remove it in overridden method in java?
- Can we change method signature in overriding in Java?
- What are the rules need to follow when overriding a method that throws an exception in Java?
- While overriding can the subclass choose not to throw an exception in java?
- Are the instances of Exception checked or unchecked exceptions in java?
- Exception handling with method overriding in Java.
- Can abstract method declaration include throws clause in java?
- Is parent child hierarchy important on throws while overriding in Java?
- Checked vs Unchecked exceptions in Java
- Is it possible to throw exception without using "throws Exception" in java?
