Java-Quiz: try, catch & finally

This Java quiz walks through try, catch and finally, including the cases that surprise people, like a return inside finally. You will also trace try-with-resources and see when resources get closed.

Mittel 10 Fragen ~6 Minuten Bestanden ab 80% Java 17

Modus wählen

Du spielst als Gast: Melde dich an, damit XP und Serie gespeichert werden.

Beispielfrage

Frage 1 von 10

What does this print?

public class Main {
    static String test() {
        try {
            return "try";
        } finally {
            System.out.print("finally ");
        }
    }

    public static void main(String[] args) {
        System.out.println(test());
    }
}
Lösung zeigen

B. finally try The return value is computed first, then finally runs before the method actually returns. So finally is printed inside test(), and try is printed afterwards by main.

Darum geht es in diesem Quiz

  • The order of try, catch, finally and the code after them
  • finally after return, and return inside finally
  • Returning a primitive vs an object from try
  • Catch block order and exceptions thrown from catch
  • Try-with-resources, AutoCloseable and close order

Häufige Fragen

Brauche ich ein Konto?

Nein. Jeder kann spielen. Mit Anmeldung werden XP, Serie, Abzeichen und Verlauf gespeichert.

Kann ich es wiederholen?

Ja. Dein Bestwert bleibt erhalten, und Fragen und Antworten werden jedes Mal neu gemischt.