Quiz

Quiz: Ausnahmen und Überschreiben

protected void methode() throws IOException { /* irrelevant */ }

protected void methode() throws IOException
protected void methode() throws FileNotFoundException, IOException
protected void methode() throws FileNotFoundException
protected void methode() throws Exception
protected void methode()
public void methode() throws FileNotFoundException, IOException
public void methode() throws FileNotFoundException
public void methode() throws Exception
public void methode()
private void methode() throws FileNotFoundException, IOException
private void methode() throws FileNotFoundException
private void methode() throws Exception
public void methode() throws ArrayIndexOutOfBoundsException
private void methode() throws ArrayIndexOutOfBoundsException
protected void methode() throws ArrayIndexOutOfBoundsException
protected int methode() throws IOException
protected void methode() throws IOException { /* irrelevant */ }

protected void methode() throws IOException
protected void methode() throws FileNotFoundException, IOException
protected void methode() throws FileNotFoundException
protected void methode() throws Exception // FALSCH!!
protected void methode()
public void methode() throws FileNotFoundException, IOException
public void methode() throws FileNotFoundException
public void methode() throws Exception // FALSCH!!
public void methode()
private void methode() throws FileNotFoundException, IOException // FALSCH!!
private void methode() throws FileNotFoundException // FALSCH!!
private void methode() throws Exception // FALSCH!!
public void methode() throws ArrayIndexOutOfBoundsException
private void methode() throws ArrayIndexOutOfBoundsException // FALSCH!!
protected void methode() throws ArrayIndexOutOfBoundsException
protected int methode() throws IOException // FALSCH!!

Ausnahmen dürfen beim Überschreiben von Methoden nicht erweitert werden, auch dürfen keine neuen (checked) Ausnahmen hinzugefügt werden. Weiterhin darf beim Überschreiben der Rückgabetyp der Methode nicht verändert werden oder deren Sichtbarkeit eingeschränkt. Runtime-Exceptions dürfen jedoch jederzeit hinzugefügt werden, da sie nicht unter die Handle-or-Declare-Regel von Java fallen. Deswegen kann man die ArrayIndexOutOfBoundsException ohne Probleme hinzufügen.

Quiz: finally

try {
    try {
        System.out.print("A");
        thrower();
        System.out.print("B");
        return;
    } catch (FileNotFoundException ex) {
        System.out.print("C");
    } finally {
        System.out.print("D");
    }
    System.out.print("E");
    return;
} finally {
    System.out.print("F");
}
// ACDEF

Was passiert, wenn thrower() eine FileNotFoundException wirft?

Das Programm gibt „A“ aus, dann wird von der Methode thrower() einen FileNotFoundException geworfen, mit der Folge, dass der entsprechende try-Block abrupt beendet wird. Die Ausführung springt dann in den entsprechenden catch-Block für die Ausnahme, in dem „C“ ausgegeben wird. Danach wird der finally-Block ausgeführt („D“) und die Ausführung geht nach dem ersten try-catch-Block weiter („E“). Trotz des return-Statements wird noch der letzte finally-Block ausgeführt („F“).


Copyright © 2025 Thomas Smits