About exceptions

Many of you are still having problems with exceptions. Here is an analogy that might help. Consider a chain of command: Each person in the chain gives commands to those under his/her immediate command. Each person in the chain reports to his/her immediate boss. Two kinds of reports are possible: Success or Failure. Since bosses don't tolerate failures very well, failures are called exceptions. Now consider an order from Bill Clinton to Al Gore. Al can't fulfill the order himself, so he asks General A to do it and report back to him. The General, in turn, asks the Major to do the job, and so on. Eventually GI Joe gets the job. If he succeeds, he reports Success to his boss, who reports Success, and so on, and Bill is happy. But what if he fails? Well, he will have to inform the Sergeant of his failure. In Java this might look like:
void GIMethod () throws FailedToDoTheJob { 
  if (todayIsSunday) throw new FailedToDoTheJob();
  else GIDoesTheJob();
}
The Sergeant has two choices: either do the job himself or report failure back to the Lieutenant. In the first case, the Sergeant catches the exception raised by the GI, does the job, and reports Success to the Lieutenant. In the second case, the Sergeant simply passes the exception. In Java, you might write this as follows:
// First case: 
void SergeantMethod () {
  try {
	GIMethod();
  }
  catch (FailedToDoTheJob) {
	SergeantDoesTheJob();
  }
}
// Second case:
void SergeantMethod () throws FailedToDoTheJob {
  GIMethod();
}

Visited times since December 15, 1997 (or the last crash).

sabry@cs.uoregon.edu