Java try, catch & finally Quiz

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.

Medium 10 questions ~6 minutes Pass at 80% Java 17

Choose a mode

Playing as a guest: sign in to keep your XP and streak.

Sample question

Question 1 of 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());
    }
}
Show the answer

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.

What this quiz covers

  • 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

Questions people ask

Do I need an account?

No. Anyone can play. Sign in to keep your XP, streak, badges and history.

Can I retake it?

Yes. Your best score is kept, and the questions and answers are shuffled each time.