Use an ascii text editor or word processor to answer questions 1-5.
Submission instructions are at the end of this assignment.
- Fill in the blanks for each of the following questions:
- _______________ The Java class that you should use as a superclass
when creating your own exceptions.
- _______________ The Java superclass that handles exceptions that
are not type checked by the compiler (e.g.,
NullPointerException)
- _______________ The Java superclass that handles errors that occur
in the Java virtual machine, such as stack overflow.
- _______________ The error handling mechanism you can use in C to
escape from a deeply nested stack of calls to a
procedure that can handle the error (this mechanism
will pop all the intervening stack frames off the
stack).
- If you want a "default" catch statement that will catch any exception
thrown by a called procedure, how would you write the catch statement in:
- Java
- C++
- In C++, when you throw an exception, why should you write:
throw FooException();
rather than:
throw new FooException();
- For each of the following code snippets, explain what is wrong with the
snippet and how you would fix it. Here are some global declarations:
class RecognitionError extends Exception { ... }
class MismatchedTokenError extends RecognitionError { ... }
class InvalidTokenError extends RecognitionError { ... }
class NoAvailableAlternativeError extends RecognitionError { ... }
class MismatchedNumberError extends MismatchedTokenError { ... }
class MismatchedOperatorError extends MismatchedTokenError { ... }
-
try {
...
}
catch (RecognitionError re) {
...
}
catch (InvalidTokenError te) {
...
}
catch (MismatchedOperatorError oe) {
...
}
-
public void evaluate () {
try {
...
throw new InvalidTokenError();
}
}
- What is the output printed by the following Java code (I know you can code
up this example and run it, but will you be able to do that on an exam?):
class NegativeNumberError extends Exception {
int index;
public NegativeNumberError(int i) {
index = i;
}
int getIndex() { return index; }
public String getMessage() {
return "Negative number at index " + index;
}
}
int sumArray(int x[]) throws NegativeNumberError {
int i;
int sum = 0;
try {
for (i = 0; i < x.length; i++) {
if (x[i] < 0) throw new NegativeNumberError(i);
sum += x[i];
}
}
catch (NegativeNumberError ne) {
System.out.println(ne.getMessage());
throw ne;
}
finally {
System.out.printf("sum = %d%n", sum);
}
return sum;
}
public int printSum() {
int numbers[] = { 10, 20, 40, -20, 30, -40 };
int sum = 0;
try {
sum = sumArray(numbers);
}
catch (NegativeNumberError ne) {
System.out.printf("Negative number is %d%n", numbers[ne.getIndex()]);
}
System.out.printf("sum = %d%n", sum);
return sum;
}
- Go to the SumLines.java
file that we discussed in class and modify it so that it keeps prompting
the user for a filename until a usable filename is provided.
- Augment your prefix expression interpreter from homework 3 by throwing
and catching the following types of errors:
- Too few operands: This error occurs when
you are parsing an expression and
exhaust the tokens on the line while expecting more tokens.
- Undefined token: This error occurs if you get an operator
other than [+-*/=] or an id that does not start with a letter
(hint: the Character class has an isLetter method that returns
true or false depending on whether a character is a letter).
- Undefined variable: This error occurs when you try to access
the value of an undefined variable.
- Bad placement of the assignment operator: This error occurs
when an assignment operator (=) occurs anywhere but as the
first token in a line.
You should take the following actions to augment your hw 3 solution:
- For each of these errors, write an exception class for the error
and override the getMessage method with an appropriate
error message.
- For undefined variables and tokens the constructor
should take the undefined variable or token and the error message
should reference it.
- Add error checking code to your solution and throw the
appropriate exception when an error is discovered.
- Catch the exceptions in your interpreter and print the
appropriate error messages. Continue to read and evaluate
expressions after handling the exception.
- Add "Throws" statements to the methods that throw but do
not handle the exceptions. You will probably find that
when you try to throw an undefined variable exception, that
you will need to add a "Throws" statement not just to the
eval method for an identifier, but also to the eval method
for its superclass.
If you have a question about how your executable should behave,
you can run my interpreter:
java -jar /home/bvz/cs365/hw/hw7/Prefix.jar
One nice thing you should notice about the exception handling
for prefix expressions is that you will often want to throw
an exception from a deeply nested, recursive parsing routine.
It is nice to be able to throw an exception all the way to the
top-level interpreter routine, without having to return through
each invocation of the parsing routine, and having to pass a
boolean flag indicating whether or not evaluation succeeded or
failed.