Use an ascii text editor or word processor to answer questions 1-5. Submission instructions are at the end of this assignment.


  1. Fill in the blanks for each of the following questions:

    1. _______________ The Java class that you should use as a superclass when creating your own exceptions.

    2. _______________ The Java superclass that handles exceptions that are not type checked by the compiler (e.g., NullPointerException)

    3. _______________ The Java superclass that handles errors that occur in the Java virtual machine, such as stack overflow.

    4. _______________ 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).

  2. 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:

    1. Java
    2. C++

  3. In C++, when you throw an exception, why should you write:
        throw FooException();
      
    rather than:
        throw new FooException();
      

  4. 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 { ... }
         
    1. 	 try {
      	    ...
      	 }
      	 catch (RecognitionError re) {
      	    ...
      	 }
      	 catch (InvalidTokenError te) {
      	    ...
      	 }
      	 catch (MismatchedOperatorError oe) {
      	    ...
      	 }
      	 
    2. 	public void evaluate () {
      	  try {
      	     ...
      	     throw new InvalidTokenError();
      	  }
      	}
            
  5. 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;
           }
    
  6. 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.

  7. Augment your prefix expression interpreter from homework 3 by throwing and catching the following types of errors:

    1. Too few operands: This error occurs when you are parsing an expression and exhaust the tokens on the line while expecting more tokens.
    2. 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).
    3. Undefined variable: This error occurs when you try to access the value of an undefined variable.
    4. 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:

    1. For each of these errors, write an exception class for the error and override the getMessage method with an appropriate error message.

    2. For undefined variables and tokens the constructor should take the undefined variable or token and the error message should reference it.

    3. Add error checking code to your solution and throw the appropriate exception when an error is discovered.

    4. Catch the exceptions in your interpreter and print the appropriate error messages. Continue to read and evaluate expressions after handling the exception.

    5. 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.


What to Submit

Submit a jar file named hw7.jar with the following contents:

  1. A hw7.txt or hw7.pdf file with your answers to questions 1-5.
  2. The .java and .class files for SumLines and BadNumberException for question 6.
  3. The .java and .class files for your solution to the prefix expression interpreter.
  4. A manifest.txt file that names your main class for the prefix expression interpreter.
We should be able to type:
java -jar hw7.jar
and have your prefix expression interpreter start up.