Method Overriding with Exceptions Tutorial

I have created several tutorials on method overriding that explain certain rules based on the current level of teaching at that time. Now that I have covered exception handling in detail, I would like to explain a few more rules with relation to overriding methods that declare thrown exceptions. First and foremost, you should have a good understanding of the difference between a checked and unchecked exception. Furthermore, you should understand the purpose of the throws list. Please watch all my exception handling tutorials if you are unsure in any way.

Declaring Unchecked Exceptions

There are no rules that pertain to unchecked exceptions and method overriding. Declaring unchecked exceptions in the throws list is completely optional – plus pointless and confusing – and will not affect your program at compilation or runtime.

Declaring Checked Exceptions

Examining the superclass method first is the key to determining what is a legal override for a subclass. Just to emphasize, the rules below pertain only to checked exceptions in a throws list.

  • If a method declaration in the superclass has no throws list, then methods in the subclass CANNOT contain a throws list.
  • If a method declaration in the superclass declares a checked exception in the throws list, then the subclass can declare the same exception in its throws list.
  • If a method declaration in the superclass declares a checked exception in the throws list, then the subclass can declare a subclass of that exception in its throws list.
  • If a method declaration in the superclass declares a checked exception in the throws list, then the subclass can have NO throws list.
  • If a method declaration in the superclass declares a checked exception in the throws list, then the subclass CANNOT declare a superclass of that exception in its throws list.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md MethodOverrideException
C:\Java>cd MethodOverrideException
C:\Java\MethodOverrideException>Notepad MethodOverrideException.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.io.*;

class MethodOverrideException {
    static String temp;

    public static void main(String args[]) {
        SuperClass super1 = new SuperClass();
        try {
            super1.uncheckedMethod("Hello World");
        } catch (Exception e) {
            System.out.println("Caught exception: " + e);
        }

        SubClass sub1 = new SubClass();
        try {
            sub1.uncheckedMethod(temp);
        } catch (Exception e) {
            System.out.println("Caught exception: " + e);
        }

        System.out.println();
        super1.checkedNoThrows();
        sub1.checkedNoThrows();

        System.out.println();
        try {
            super1.checkedThrows();
        } catch (Exception e) {
            System.out.println("Caught exception: " + e);
        }

        try {
            sub1.checkedThrows();
        } catch (Exception e) {
            System.out.println("Caught exception: " + e);
        }

    }
}

class SuperClass {
    void uncheckedMethod(String s) throws NullPointerException, ArithmeticException { // throws list unchecked exceptions are optional and pointless
        System.out.println(s.substring(200,300)); // StringIndexOutOfBoundsException
    }

    void checkedNoThrows() {
        System.out.println("Invoked the checkedNoThrows superclass method.");
    }

    void checkedThrows() throws IOException {
        throw new IOException();
    }
    
}

class SubClass extends SuperClass {
    void uncheckedMethod(String s) throws IndexOutOfBoundsException { /// throws list unchecked exceptions are optional and pointless
        System.out.println(s.length()); // NullPointerException
    }   
    
    //void checkedNoThrows() throws IOException {
    void checkedNoThrows() {
        System.out.println("Invoked the checkedNoThrows subclass method.");
    }  
    
    void checkedThrows() throws IOException { // YES - throws list is the same
        System.out.println("Invoked the checkedThrows subclass method.");
        throw new IOException();
    }      
    
    //void checkedThrows() throws EOFException { // YES - throws list contains subclass of IOException
    //    throw new EOFException();
    //}   

    //void checkedThrows() { // YES - missing throws list completely
    //    System.out.println("Invoked the checkedThrows subclass method.");
    //}

    //void checkedThrows() throws Exception { // NO - Exception is the superclass of IOException - not allowed
    //    System.out.println("Invoked the checkedThrows subclass method.");
    //    throw new Exception();
    //}
}

Now switch back to the command prompt (CMD) and type in javac MethodOverrideException.java and press Enter.
Now type in java MethodOverrideException and press Enter.


C:\Java\MethodOverrideException>javac MethodOverrideException.java
C:\Java\MethodOverrideException>java MethodOverrideException
Caught exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 300
Caught exception: java.lang.NullPointerException

Invoked the checkeNoThrows superclass method.
Invoked the checkeNoThrows subclass method.

Caught exception: java.io.IOException
Invoked the checkedThrows subclass method.
Caught exception: java.io.IOException


Final thoughts

None


Tutorials