A good answer might be:

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


NumberFormatException

Here is the program that received bad input. When the user entered "Rats", parseInt() threw a NumberFormatException object.

import java.lang.* ;
import java.io.* ;

public class Square 
{

  public static void main ( String[] a ) throws IOException
  {
    BufferedReader stdin = 
        new BufferedReader ( new InputStreamReader( System.in ) );
 
    String inData;
    int    num ;

    System.out.println("Enter an integer:");
    inData = stdin.readLine();

    num    = Integer.parseInt( inData );     // convert inData to int

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

  }
}

But the program lacks code to deal with this exception. Instead, when an exception is thrown, the program throws the exception to its caller. That is what the clause throws IOException means. When the Java virtual machine receives an exception from a program, it stops the program and prints a trace of what went wrong.

QUESTION 4:

Could statements be added to handle this exception?