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
    }
}

Quand j'ai jeter les deux types d'exception (IOException dans Line4 et RunTimeException dans Line3), j'ai trouvé que mon programme ne compile pas jusqu'à ce que j'indique "IOException" dans mon jette des clauses de la Ligne 1 & Ligne 2.

Alors que si j'inverse "jette" pour indiquer IOException jetées, le programme ne compile correctement, comme indiqué ci-dessous.

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
    }
}

Pourquoi dois-je toujours utiliser "jette" pour IOException même si RuntimeException est également levée (Ligne 3) ?

OriginalL'auteur UnderDog | 2013-08-24