No. You only write the catch{}
blocks for the exceptions you
wish to handle.
The other exceptions are passed up to the caller.
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.lang.* ;
import java.io.* ;
public class SquareUser
{
public static void main ( String[] a ) throws IOException
{
BufferedReader stdin =
new BufferedReader ( new InputStreamReader( System.in ) );
String inData = null;
int num = 0;
boolean goodData = false;
while ( !goodData )
{
System.out.println("Enter an integer:");
inData = stdin.readLine();
try
{
num = Integer.parseInt( inData );
goodData = true;
}
catch (NumberFormatException ex )
{
System.out.println("You entered bad data." );
System.out.println("Please try again.\n" );
}
}
System.out.println("The square of " + inData + " is " + num*num );
}
}
This is a common style of reading user input. It would be uncommonly useful to copy, save, and run this program.