[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
[CS152 2005S]
[CS152 2005F]
Back to Loops. On to Documentation.
Held: Wednesday, February 15, 2006
Summary: Today we consider the problems that may occur in programs and procedures and techniques for indicating and recovering from those problems, particularly Java's exceptions.
Related Pages:
Assignments
Notes:
Overview:
the outside world.
fifty percent" rather
than 0.50 in response to a request for the
successful serve percentage in a raquetball simulator.
int DONE = -1;
boolean done = false;
int count = 0;
int sum = 0;
int grade;
while (!done) {
out.print("Enter grade: ");
grade = in.readInt();
if (grade != DONE) {
sum = sum + grade;
count = count + 1;
}
else {
done = true;
}
} // while
out.println("The average grade is " + (sum/count));
member returns #f when it
can't find the value.
I'll fix it once I get the 'real' stuff working; and
When you try to read an integer you may fail because the user enters a non-number, you might write something like:
/**
* Read an integer.
*
* @exception NumberFormatException
* if the user enters something other than an integer
*/
public int readInt()
throws NumberFormatException
// ...
throws the exception.
public static void main(String[] args)
throws NumberFormatException
{
// ...
int row = in.readInt();
// ...
}
try clause.
catch clause.
try {
// stuff that may have problems
}
catch (ExceptionClass e1) {
// Handle one type of exception
}
catch (AnotherExceptionClass e2) {
// Handle another type of exception
}
finally {
// Clean-up code that is always executed.
}
public static void main(String[] args)
throws Exception
{
// ...
int row = -1;
while (row < 0) {
try {
row = in.readInt();
}
catch (NumerFormatException e) {
out.println("Not a number");
}
} // while
// ...
} // main
java.io.IOException and
java.lang.NumberFormatException.
Back to Loops. On to Documentation.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
[CS152 2005S]
[CS152 2005F]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue May 9 08:31:33 2006.
The source to the document was last modified on Thu Jan 12 14:58:06 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2006S/Outlines/outline.15.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby