levo

Designing, Testing and Evaluating an Event Driven Application

Using try and catch inside an event handler

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

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.

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