IOException vs RuntimeException Java

class Y {
    public static void main(String[] args) throws RuntimeException{//Line 1
        try {
            doSomething();
        }
        catch (RuntimeException e) {
            System.out.println(e);
        }
    }
    static void doSomething() throws RuntimeException{ //Line 2
        if (Math.random() > 0.5) throw new RuntimeException(); //Line 3
        throw new IOException();//Line 4
    }
}

Beim Auslösen zweier Ausnahmetypen (IOException in Zeile 4 und RunTimeException in Zeile 3) stellte ich fest, dass mein Programm erst dann kompiliert wird, wenn in den Throws-Klauseln in Zeile 1 und Zeile 2 "IOException" angegeben ist.

Wenn ich dagegen "throws" rückgängig mache, um anzuzeigen, dass IOException ausgelöst wird, wird das Programm wie unten gezeigt erfolgreich kompiliert.

class Y {
    public static void main(String[] args) throws IOException {//Line1
        try {
            doSomething();
        }
        catch (RuntimeException e) {
            System.out.println(e);
        }
    }
    static void doSomething() throws IOException {//Line 2
        if (Math.random() > 0.5) throw new RuntimeException();//Line 3
        throw new IOException();//Line 4
    }
}

Warum sollte ich für IOException immer "throws" verwenden, obwohl auch RuntimeException ausgelöst wird (Zeile 3)?

Antworten auf die Frage(2)

Ihre Antwort auf die Frage