[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
Back to Loops. On to Exceptions, Continued.
Held Wednesday, September 15, 1999
Overview
Today, we will consider exceptions, Java's standard mechanism that permits programmers to indicate, discover, and recover from some errors in programs.
Notes
Contents
Summary
Mailbox
object that communicates with the mail server and gives us simple
methods for accessing the mail. To create a mailbox for a session,
you need an account and password. What should happen in the following
if the user enters an incorrect account or password?
out.print("Enter account: ");
account = in.readString();
out.print("Enter password: ");
password = in.readString();
Mailbox mail = new Mailbox(account, password);
MessageInfoList message = mail.getMessages();
...
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
if (!Mailbox.isvalid(account,password)) {
out.println("Invalid account or password.");
}
else {
// rest of program
}
if (count == 0) {
out.println("In order to compute an average grade, " +
"I must have at least one grade.");
}
else {
out.println("The average grade is " + sum/count);
}
// Assumption: The empty account with the empty password is
// not a valid account.
String account = "";
String password = "";
while (!Mailbox.isvalid(account,password)) {
out.print("Enter account: ");
account = in.readString();
out.print("Enter password: ");
password = in.readString();
}
Mailbox mail = new Mailbox(account, password);
Mailbox mail = new Mailbox(account, password);
if (!mail.isvalid()) {
out.println("Invalid account. Exiting ...");
}
else {
// rest of program
}
int average = sum / count;
if (INVALID_MATHEMATICAL_OPERATION) {
out.println("Could not compute the average.");
}
else {
out.println("The average grade is " + average);
}
/**
* Create a new mailbox for a particular account, using the account
* name and password. Uses the default mail server, specified in
* Config.
*
* @exception InvalidAccount
* If the account or password is invalid.
* @exception ServerError
* If unable to connect to the server.
*/
public Mailbox(String account, String password)
throws InvalidAccount, ServerError
{
// Code for creating the mailbox.
} // Mailbox(String, String)
throws the exception.
public static void main(String[] args)
throws InvalidAccount, ServerError
{
// ...
Mailbox mail = new Mailbox(account, pasword);
// ...
}
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) {
// ...
Mailbox mail;
try {
mail = new Mailbox(account, password);
}
catch (InvalidAccount ia) {
out.println("Invalid account or password.");
System.exit(1);
}
catch (ServerError se) {
out.println("Having difficulties connecting to the server.");
out.println("Try again later.");
System.exit(2);
}
// ...
} // main
java.io.IOException and
java.lang.NumberFormatException.
Tuesday, 10 August 1999
Wednesday, 15 September 1999
Back to Loops. On to Exceptions, Continued.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99F/Outlines/outline.12.html
Source text last modified Wed Sep 15 15:47:22 1999.
This page generated on Tue Sep 21 11:10:02 1999 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu