[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]
Back to Cancelled. On to Exceptions Lab.
Held: Monday, February 21, 2005
Summary: Today we discuss the problems that may occur in programs and procedures and techniques for indicating and recovering from those problems, particularly Java's exceptions.
Related Pages:
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 Cancelled. On to Exceptions Lab.
[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]
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 Wed May 11 10:55:54 2005.
The source to the document was last modified on Mon Jan 24 10:17:07 2005.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2005S/Outlines/outline.17.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby