levo

Exception Handling and Managing Input and Output

The finally block and releasing resources

  • 3 things to know
  • AS Unit 1
Start revising · £5/month

Overview

Finally block and resource leak.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

Why is closing a file in a finally block safer than closing it at the end of the try block?

Key term

Why might closing a file itself need to be inside a try block?

Key term

What is a finally block normally used for?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is a finally block?

    Model answer

    A finally block is written after the catch blocks and its statements run whether or not an exception was thrown, so it holds work that must happen either way.

  2. 2

    What is a finally block normally used for?

    Model answer

    A finally block normally releases resources, closing an open file, a database connection or a network socket, so nothing is left held after the method finishes.

  3. 3

    Does a finally block run when no exception occurs?

    Model answer

    The finally block runs after the try block has completed normally, just as it runs after a catch block has dealt with a fault.

  4. 4

    Does a finally block run when the exception is not caught at all?

    Model answer

    The finally block still runs as the method is abandoned, so the clean-up happens before the exception travels out to the calling method.

  5. 5

    Does a finally block run when the try block executes a return?

    Model answer

    The finally block runs before the method actually returns, so a value on its way back to the caller does not allow the clean-up to be skipped.

  6. 6

    A program runs try { System.out.println("1"); throw new Exception(); } catch (Exception e) { System.out.println("2"); } finally { System.out.println("3"); } and then System.out.println("4");. What is printed?

    Model answer

    The try prints 1 and then throws, so the handler prints 2, the finally prints 3 because it always runs, and execution continues to print 4, giving 1, 2, 3 and 4.

Other ways to work this

The same content, four other ways.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

Software and the Development Environment
Defining Data: Types, Variables and Classes
Program Control Structures
Objects, Classes and Data Structures

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

A null is the of one.
absencepresencetype

Blank Blitz consolidates the facts

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

An error is the mistake a person made.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Exception Handling and Managing Input and Output, on a schedule.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.