Answer:

No ― none of our previous example programs have done this.

InputMismatchException

Here is the program that received bad input. When the user entered "rats", nextInt() threw a InputMismatchException object.

import java.util.* ;

public class Square
{

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

    System.out.print("Enter an integer: ");
    num = scan.nextInt();      
    System.out.println("The square of " + num + " is " + num*num );
  }
}

But the program lacks code to deal with this exception. Instead, when nextInt() throws an exception, the program throws the exception to the Java virtual machine. When the Java virtual machine catches the exception, it stops the program and prints a trace of what went wrong.

QUESTION 4:

Could statements be added to handle this exception?