levo

Exception Handling and Managing Input and Output

The try and catch block

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

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.

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.