More Compact Diagram
A more compact way of showing the hierarchy is:
Exception
IOException
AWTException
RunTimeException
ArithmeticException
IllegalArgumentException
NumberFormatException
IndexOutOfBoundsException
Others
Others
Don't memorize this diagram (for one thing, it is not complete).
But look at it to see what it says.
Here is a possibly incorrect program:
import java.lang.* ;
import java.io.* ;
public class DivisionPractice
{
public static void main ( String[] a ) throws IOException
{
BufferedReader stdin =
new BufferedReader ( new InputStreamReader( System.in ) );
String inData;
int num=0, div=0 ;
try
{
System.out.println("Enter the numerator:");
inData = stdin.readLine();
num = Integer.parseInt( inData );
System.out.println("Enter the divisor:");
inData = stdin.readLine();
div = Integer.parseInt( inData );
System.out.println( num + " / " + div + " is " + (num/div) );
}
catch (NumberFormatException ex )
{
System.out.println("You entered bad data." );
System.out.println("Run the program again." );
}
catch (ArithmeticException ex )
{
System.out.println("You can't divide " + num + " by " + div);
}
}
}
|