[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Links]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.4.2 API]
[EIJ]
[CS152 2000F]
[CS153 2004S]
Back to Reuse through Inheritance. On to Exceptions (2).
Held: Monday, 27 September 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
.
Back to Reuse through Inheritance. On to Exceptions (2).
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Links]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.4.2 API]
[EIJ]
[CS152 2000F]
[CS153 2004S]
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 Dec 8 10:37:13 2004.
The source to the document was last modified on Thu Aug 26 20:22:22 2004.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2004F/Outlines/outline.18.html
.
You may wish to
validate this document's HTML
;
;
Check with Bobby