Computer Science Fundamentals (CS153 2004S)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Instructions]
[Links]
[Search]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[Experiments in Java]
[Java API]
[Scheme Reference]
[Scheme Report]
[CS153 2003S]
[CS151 2003F]
[CS152 2000F]
[SamR]
Back to Loops. On to Building a Rational Class, Revisited.
Held: Thursday, 1 April 2004
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:
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.
Java is your nanny!
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:
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) {
// ...
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.
throw new MyException(message);
public int readInt()
throws Exception
{
String str = this.readString();
if (!containsOnlyDigits(str))
throw new Exception("'" + str + "' is not a number");
// ...
} // readInt()
public class MyException
extends Exception
{
} // class MyException
MyException.
public class MyException
extends Exception
{
public MyException(String msg) {
super(msg);
} // MyException(String)
} // class MyException
When someone creates aMyExceptionwith a String as a parameter, use that string as the parameter to the constructor ofException(my superclass).
Back to Loops. On to Building a Rational Class, Revisited.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Instructions]
[Links]
[Search]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[Experiments in Java]
[Java API]
[Scheme Reference]
[Scheme Report]
[CS153 2003S]
[CS151 2003F]
[CS152 2000F]
[SamR]
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 Fri May 7 09:43:23 2004.
The source to the document was last modified on Tue Jan 13 10:26:11 2004.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2004S/Outlines/outline.34.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby