levo

Exception Handling and Managing Input and Output

The try and catch block

  • 3 things to know
  • AS Unit 1

Overview

Enclosing the statements that may fail, what happens to the rest of the block once an exception is thrown, catching by type, and what a handler should do besides reporting.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

What goes inside a try block?

Key term

What happens when the statements in a try block all succeed?

Key term

Why must a variable that is used after a try block be declared before it?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What goes inside a try block?

    Model answer

    A try block holds the statements that might fail, grouped together so that a fault in any of them can be dealt with in one place.

  2. 2

    What goes inside a catch block?

    Model answer

    A catch block holds the code that responds to a fault, and its heading names the type of exception it will handle and a variable through which the exception object can be read.

  3. 3

    Write out the general shape of a try and catch in Java.

    Model answer

    The construct is written try, then the statements that might fail in braces, then catch with an exception type and a variable in brackets, then the statements that handle the fault in braces.

  4. 4

    What happens to the rest of a try block once an exception is thrown part way through it?

    Model answer

    The remaining statements of the try block are abandoned at once and control passes to the matching catch block, so no later statement inside the try is executed.

  5. 5

    A program runs try { System.out.println("A"); int x = 5 / 0; System.out.println("B"); } catch (ArithmeticException e) { System.out.println("C"); } and then System.out.println("D");. What is printed?

    Model answer

    The letter A is printed, then the division by zero throws before B can run, so control jumps to the handler and prints C, and execution continues after the block to print D, giving A, C and D.

  6. 6

    What happens when the statements in a try block all succeed?

    Model answer

    The whole try block runs, every catch block is skipped, and execution continues at the first statement after the construct.

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.