Upload
godwin-bruce
View
213
Download
1
Embed Size (px)
Citation preview
CSC 243 – Java Programming, Spring, 2009
Week 9Java’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.
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);– }– }
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.
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.