There are few things to remember when overriding a method with exception handling because method of super class may have declared exception.
There are some rules we have to follow while overriding method with exception handling. The Rules are as follows:
- superclass method does not declare an exception
- subclass overridden method can declares no exception
- subclass overridden method can declare unchecked exception(s)
- subclass overridden method cannot declare checked exception(s)
- superclass method declares checked exception
- subclass overridden method can declares no exception
- subclass overridden method can declare same exception(s)
- subclass overridden method can declare child exception(s)
- subclass overridden method cannot declare parent exception(s)
- superclass method declares unchecked exception
- subclass overridden method can declares no exception
- subclass overridden method can declare same exception(s)
- subclass overridden method can declare child exception(s)
- subclass overridden method can declare parent exception(s)
Let us discuss these rules with examples. In the following example code itself we commented what is allowed and what is not allowed.
Example 01 : superclass method does not declare an exception
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
package com.vidvaan.corejava.exception26.method.override; import java.io.IOException; // superclass method does not declare an exception class Parent01 { // method does not declare an exception public void method1() { System.out.println("Parent01.method1()"); } // method does not declare an exception public void method2() { System.out.println("Parent01.method2()"); } // method does not declare an exception public void method3() { System.out.println("Parent01.method3()"); } // method does not declare an exception public void method4() { System.out.println("Parent01.method4()"); } // method does not declare an exception public void method5() { System.out.println("Parent01.method5()"); } } class Child01 extends Parent01 { // subclass overridden method can declares no exception @Override public void method1() { System.out.println("Child01.method1()"); } // subclass overridden method can declare unchecked exception @Override public void method2() throws ArithmeticException { System.out.println("Child01.method2()"); } // subclass overridden method can declare unchecked exceptions @Override public void method3() throws NullPointerException, ArithmeticException { System.out.println("Child01.method3()"); } // subclass overridden method cannot declare checked exception // Compilation Error : Exception IOException is not compatible with throws // clause in Parent01.method4() @Override public void method4() throws IOException { System.out.println("Child01.method4()"); } // subclass overridden method cannot declare checked exceptions // Compilation Error : Exception ClassNotFoundException is not compatible with // throws clause in Parent01.methodThree() @Override public void method5() throws ClassNotFoundException, IOException { System.out.println("Child01.method5()"); } } public class SuperClassMethodNotDeclaresException { public static void main(String args[]) { Parent01 parent = new Child01(); parent.method1(); parent.method2(); parent.method3(); parent.method4(); parent.method5(); } } |
Example 02 : superclass method declares checked exception
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
package com.vidvaan.corejava.exception26.method.override; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InterruptedIOException; // superclass method declares checked exception class Parent02 { // method declares checked exception public void method1() throws IOException { System.out.println("Parent02.method1()"); } // method declares checked exception public void method2() throws IOException { System.out.println("Parent02.method2()"); } // method declares checked exception public void method3() throws IOException { System.out.println("Parent02.method3()"); } // method declares checked exception public void method4() throws IOException { System.out.println("Parent02.method4()"); } // method declares checked exception public void method5() throws IOException { System.out.println("Parent02.method5()"); } // method declares checked exception public void method6() throws IOException { System.out.println("Parent02.method6()"); } // method declares checked exception public void method7() throws IOException { System.out.println("Parent02.method7()"); } } class Child02 extends Parent02 { // subclass overridden method can declares no exception @Override public void method1() { System.out.println("Child02.method1()"); } // subclass overridden method can declare same exception @Override public void method2() throws IOException { System.out.println("Child02.method2()"); } // subclass overridden method can declare same exceptions @Override public void method3() throws IOException, IOException { System.out.println("Child02.method3()"); } // subclass overridden method can declare child exception @Override public void method4() throws FileNotFoundException { System.out.println("Child02.method4()"); } // subclass overridden method can declare child exceptions @Override public void method5() throws InterruptedIOException, FileNotFoundException { System.out.println("Child02.method5()"); } // subclass overridden method cannot declare parent exception // Compilation Error : Exception Exception is not compatible with throws clause // in Parent02.method6() @Override public void method6() throws Exception { System.out.println("Child02.method6()"); } // subclass overridden method cannot declare parent exceptions // Compilation Error : Exception Throwable is not compatible with throws clause // in Parent02.method7() @Override public void method7() throws Throwable, Exception { System.out.println("Child02.method7()"); } } public class SuperClassMethodDeclaresCheckedException { public static void main(String args[]) throws IOException { Parent02 parent = new Child02(); parent.method1(); parent.method2(); parent.method3(); parent.method4(); parent.method5(); parent.method6(); parent.method7(); } } |
Example 03 : superclass method declares unchecked exception
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
package com.vidvaan.corejava.exception26.method.override; // superclass method declares unchecked exception class Parent03 { // method declares unchecked exception public void method1() throws IndexOutOfBoundsException { System.out.println("Parent03.method1()"); } // method declares unchecked exception public void method2() throws IndexOutOfBoundsException { System.out.println("Parent03.method2()"); } // method declares unchecked exception public void method3() throws IndexOutOfBoundsException { System.out.println("Parent03.method3()"); } // method declares unchecked exception public void method4() throws IndexOutOfBoundsException { System.out.println("Parent03.method4()"); } // method declares unchecked exception public void method5() throws IndexOutOfBoundsException { System.out.println("Parent03.method5()"); } // method declares unchecked exception public void method6() throws IndexOutOfBoundsException { System.out.println("Parent03.method6()"); } // method declares unchecked exception public void method7() throws ArrayIndexOutOfBoundsException { System.out.println("Parent03.method7()"); } } class Child03 extends Parent03 { // subclass overridden method can declares no exception @Override public void method1() { System.out.println("Child03.method1()"); } // subclass overridden method can declare same exception @Override public void method2() throws IndexOutOfBoundsException { System.out.println("Child03.method2()"); } // subclass overridden method can declare same exceptions @Override public void method3() throws IndexOutOfBoundsException, IndexOutOfBoundsException { System.out.println("Child03.method3()"); } // subclass overridden method can declare child exception @Override public void method4() throws ArrayIndexOutOfBoundsException { System.out.println("Child03.method4()"); } // subclass overridden method can declare child exceptions @Override public void method5() throws StringIndexOutOfBoundsException, ArrayIndexOutOfBoundsException { System.out.println("Child03.method5()"); } // subclass overridden method cannot declare parent exception @Override public void method6() throws RuntimeException { System.out.println("Child03.method6()"); } // subclass overridden method cannot declare parent exceptions @Override public void method7() throws ArrayIndexOutOfBoundsException, RuntimeException { System.out.println("Child03.method7()"); } } public class SuperClassMethodDeclaresUnCheckedException { public static void main(String args[]) { Parent03 parent = new Child03(); parent.method1(); parent.method2(); parent.method3(); parent.method4(); parent.method5(); parent.method6(); parent.method7(); } } |