5
CSC 243 – Java Programming, Spring, 2009 Week 9 Java’s Event Infrastructure

CSC 243 – Java Programming, Spring, 2009 Week 9 Java’s Event Infrastructure

Embed Size (px)

Citation preview

Page 1: CSC 243 – Java Programming, Spring, 2009 Week 9 Java’s Event Infrastructure

CSC 243 – Java Programming, Spring, 2009

Week 9Java’s Event Infrastructure

Page 2: CSC 243 – Java Programming, Spring, 2009 Week 9 Java’s Event Infrastructure

A Chained Exception

• A chained Exceptions tacks a detail message onto an underlying cause Exception.– } catch (GameException queryx) {– throw new GameToMidiException (

– “Error querying Scrabble game”, queryx);

• Used to prepend a context-specific message to an Exception thrown from a called method.

• Your assignment 2 must throw a chained GameToMidiException for any GameException or exception from the javax.sound.midi library that you catch.

Page 3: CSC 243 – Java Programming, Spring, 2009 Week 9 Java’s Event Infrastructure

Custom Exceptions

• A custom Exception inherits, directly or indirectly, from java.lang.Exception.– public class GameToMidiException extends Exception {– public GameToMidiException(String text) {– super(text);– }– public GameToMidiException(String text, Throwable cause) { //

Constructor for the chained exception.– super(text, cause);– }– }

Page 4: CSC 243 – Java Programming, Spring, 2009 Week 9 Java’s Event Infrastructure

Event-driven Programming

• Event-driven program is not driven by a sequence of imperative program statements.

• Instead, a program constructs a set of event handlers that can respond to external events.

• User interface actions (mouse click, key click, timer, …)• State change in an event-driven simulation

• The program then connects the event sources to the its events handlers, waits for events.

• An event source later sends an event object.

Page 5: CSC 243 – Java Programming, Spring, 2009 Week 9 Java’s Event Infrastructure

Java Event Mechanisms

• Interface java.util.EventListener tags an Object that implements that interface as an Object that can handle an event.

• Most of the library EventListeners are handlers for GUI events.

• Class java.util.EventObject is a helper base class that provides some storage for an event.

• Many of these are GUI oriented as well.

• Event sources do not have a special interface.