Lecture notes for week 9, 23 October
Download
Report
Transcript Lecture notes for week 9, 23 October
CSC 243 – Java Programming,
Fall, 2008
Thursday, October 23, start of week
9, Exceptions, Events, and Generic
Classes
Exception constructs already in use
• try {
• attempt to run a block of code
• } catch (Type_A_Exception taex) {
• Do something with taex, e.g., taex.getMessage()
• } catch (Type_A_Exception tbex) {
• Do something with tbex, e.g., print, then throw tbex
• } finally {
• Do this after all of the above, even after a return!
• }
Unchecked and checked exceptions
• javac forces client code to catch checked
exceptions
• unchecked exceptions need not be caught;
they can be
– java.lang.Error
• java.io.IOError – maybe a disk drive goes off line
– java.lang.RuntimeException
• java.lang.NullPointerException
• Other java.lang.Exceptions are checked
Throwing a new exception
• An explicit throw creates a new Exception.
– public int deleteTiles(String tileset) throws
ScrabbleException
• throw new ScrabbleException("Player " + name
•
+ " does not have " + tile.toString()
•
+ " to delete from set of tiles.");
Rethrowing an exception
• A rethrow catches and handles an Exception
object, then throws it again.
• } catch (NumberFormatException nx) {
•
System.err.println(“Exception: “
•
+ nx.getMessage();
•
nx.printStackTrace(); // to System.err
•
throw nx ; // This is the rethrow
An implicit throw
• An implicit throw allows a called method to
throw an Exception via the calling method.
– public String [][] move(String command) throws
ScrabbleException
• Invokes “int used =
players[nextPlayer].deleteTiles(validword);
• But move() does not catch deleteTiles’s exception:
– public int deleteTiles(String tileset) throws ScrabbleException
• The remove() implicitly throws deleteTile’s exception.
A Chained Exception
• A chained Exceptions tacks a detail message
onto an underlying cause Exception.
– } catch (NumberFormatException nx) {
–
throw new Exception(“detail message”, nx);
• Used to prepend a context-specific message to
an Exception thrown from a called method.
• The new Exception must have the appropriate
constructor. It may be a custom Exception.
Custom Exceptions
• A custom Exception inherits, directly or
indirectly, from java.lang.Exception.
– public class ScrabbleException extends Exception
• public ScrabbleException(String text)
– super(text); // Call to base class constructor.
• Client code can explicitly catch a custom
Exception.
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.
Adding Events to Scrabble
• ~parson/JavaLang/evtscrabble
• public class ScrabbleScoreEvent extends
java.util.EventObject
• Adds a message and score to a basic EventObject.
• interface ScrabbleScoreEventListener extends
java.util.EventListener
• void notifyHighEvent(ScrabbleScoreEvent event)
• A class that implements ScrabbleScoreEventListener
provides code for notifyHighEvent.
Sending Events
• The board sends an Event every time a new
highest PUT occurs. A player sends an Event
when that player pulls into the lead.
• ScrabbleBoard.putWord
• if (score > highscore) {
•
// See lines 79, 109-111, 380-386
• ScrabblePlayer.addScore
• if (score > leadscore) {
•
// See lines 20, 49-52, 194-203
Receiving Events
• Inside class ScrabbleUI:
•
•
•
•
private static class ScrabbleUIListener
implements ScrabbleScoreEventListener {
public void notifyHighEvent(ScrabbleScoreEvent event)
game = new ScrabbleGame(playernames, filetorestore,
seed, System.out, System.err, new
ScrabbleUIListener());
• ScrabbleGame passes ScrabbleUIListener reference
down to ScrabbleBoard and ScrabblePlayer.