CS111 Lab 8

Exception Handling
File IO



Notes: Java includes two basic classes of errors: Exception and Error.
      Exception
          IOException
          RuntimeException
          ArithmeticException
          IndexOutOfBoundsException
          ArrayIndexOutOfBoundsException
          NoSuchElementException
          InputMismatchException
          IllegalArgumentException
          etc.
      Error
          VirtualMachineError
          OutOfMemoryError
          InternalError
          etc.



Part I

Examine the code below.
      import java.util.InputMismatchException;
      import java.util.Scanner;

      public class MainApp {
      	public static void main(String[] args) {
      		int x, y;
      		int result;
      		Scanner in = new Scanner(System.in);
      		try {
      			x = in.nextInt();
      			y = in.nextInt();
      			System.out.println("BLUE");
      			result = x / y;
      			System.out.println(result);
      		}
      		catch (ArithmeticException e) {
      			System.out.println("Error RED");
      		}
      		catch (InputMismatchException e) {
      			System.out.println("Error BLACK");
      		} finally {
      			System.out.println("GREEN");
      		}
      	}
      }

    

Determine the output for the input below.
  1. 4 2
  2. 4.5 2
  3. 0 4
  4. 4 0




Part II


Exercise 1

    Exception Handling using throw
  1. Write a statement that throws an appropriate exception when the index for an array is out of bounds.
  2. Is the above statement a practical example of exception handling?
  3. What are good examples of exception handling?



Exercise 2


    Write a program to create a text file containing textual content. Try an ASCII bull's eye.
    Use throws IOException to interrupt a failed I/O operation.

  1. Create an output file stream object, FileOutputStream, and link it to the text file.
  2. Write text to the file
  3. Close the file.



Exercise 3

    Write a program that reads from a text file and counts the number of paragraphs in the document.
    Perform Exception Handling using throws IOException

  1. Go to the New York Times and copy the text for an article. Make sure the text contains at least three paragraphs. Paste the text into a plain text (ascii text file) document (data.txt) and save it. Note the path.
  2. Recall that Scanners read from a given file or input stream.
    Use throws IOException to interrupt a failed I/O operation.
    The following tasks are used when reading from a file:



Exercise 4


    Write a program that accepts and displays an array of six integers.
    Use a try/catch block to circumvent user data entry errors.
    If the user enters a character or a floating-point number in response to a nextInt() method call, the program should not crash.



Exercise 5


    Write a program that requires the user to input two integers, x and y.
    Compute x divided by y.
    The program must be able to catch two exceptions, one for arithmetic and one for InputMismatchException.



Exercise 6


    Write a program that reads from a text file and counts the number of words in the document.
    Perform Exception Handling using try/catch
    1. Catch the exception when a file is not found: FileNotFoundException exception
    2. Catch the exception when a file contains invalid data: NoSuchElementException exception



Exercise 7


    Occasionally, action is needed whether or not an exception is thrown.
    The finally construct is used to handle this situation.
    Once a tryblock is entered, the statements in a finally clause are guaranteed to be executed,
    whether or not an exception is thrown.
    1. Provide a program example that uses a finally clause.
    2. Test this example.



Exercise 8


    Examine the image below.

    Provide a program that displays the printStack for an exception in this hierarchy.



Exercise 9


    Write an exception class for detecting an invalid exam score.