levo

Exception Handling and Managing Input and Output

The finally block and releasing resources

  • 3 things to know
  • AS Unit 1

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.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

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

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A null is the of one.
absencepresencetype

Blank Blitz makes facts automatic

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

AA null means no value has been recorded.
BZero and an empty string are values.
CA null is the absence of a value.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

An error is the mistake a person made.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

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.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

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