Use an ascii text editor or word processor to answer questions 1-5. Submission instructions are at the end of this assignment.
throw FooException();
rather than:
throw new FooException();
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();
}
}
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;
}
You should take the following actions to augment your hw 3 solution:
If you have a question about how your executable should behave, you can run my interpreter:
java -jar /home/bvz/cs365/hw/hw7/Prefix.jarOne 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.
Submit a jar file named hw7.jar with the following contents:
java -jar hw7.jarand have your prefix expression interpreter start up.