Answer:

No. You only write the catch{} blocks for the exceptions you wish to handle. The other exceptions are passed up to the caller.

User-friendly Code

Exception handling is important for user-friendly programs. Here is the compute-the-square program again, this time written so that the user is prompted again if the input is bad:

import java.util.* ;

public class SquareUser 
{

  public static void main ( String[] a ) 
  {
    Scanner scan = new Scanner( System.in  );
    int num = 0 ;
    boolean goodData = false;

    while ( !goodData )
    {
      System.out.print("Enter an integer: ");
      try
      {
        num      = scan.nextInt();      
        goodData = true;
      } 

      catch (InputMismatchException  ex )
      { 
        System.out.println("You entered bad data." );
        System.out.println("Please try again.\n" );
        String flush = scan.next();
      }
    }

    System.out.println("The square of " + num + " is " + num*num );

  }
}

This is a common style for reading user input. It would be useful to copy, save, and run this program.

QUESTION 8:

Could the following statement be moved into the try{} block?

System.out.println("Enter an integer:");