levo

Designing, Testing and Evaluating an Event Driven Application

Using try and catch inside an event handler

  • 3 things to know
  • AS Unit 2

Overview

Catching a failure raised by what the user typed, reporting it in a message box rather than to a console, closing anything the handler opened, and the state the form is left in.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

What does a try block contain?

Key term

What does a catch block do?

Key term

What is a finally block for?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What does a try block contain?

    Model answer

    A try block contains the statements that might fail, and the runtime watches them so a fault raised inside is passed to the matching catch.

  2. 2

    What does a catch block do?

    Model answer

    A catch block receives an exception of a stated type and holds the code that deals with it, and execution then continues after the block.

  3. 3

    What is a finally block for?

    Model answer

    A finally block runs whether or not an exception occurred, so a file or connection opened in the try is closed exactly once.

  4. 4

    A click handler contains try { int n = Integer.parseInt(txtQty.getText()); lbl.setText("Ordered " + n); } catch (NumberFormatException ex) { lbl.setText("Enter a whole number"); } and the user types 5 then clicks. What does the label read?

    Model answer

    The label reads Ordered 5, because parseInt converts the characters successfully, the setText line runs, and the catch block is skipped.

  5. 5

    The handler try { int n = Integer.parseInt(txtQty.getText()); lbl.setText("Ordered " + n); } catch (NumberFormatException ex) { lbl.setText("Enter a whole number"); } runs while the box holds the word two. What does the label read?

    Model answer

    The label reads Enter a whole number, because parseInt throws, the setText line inside the try is never reached, and the catch block runs instead.

  6. 6

    Why should a catch block name a specific exception type rather than catch every exception?

    Model answer

    Naming a specific type deals only with the fault expected there, so a different and unforeseen fault is not hidden behind the wrong message.

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 Designing, Testing and Evaluating an Event Driven Application, 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.