A good answer might be:

The stack trace shows the situation:

  • at the time the exception happened.

Throwing an Exception

Your program can throw an exception when it detects a problem situation. The problem can be one that is unique to the program's application. For example, say a program for an insurance company calculates a person's age by subtracting their birthyear from the current year 2000, and then calculates the number of years they have been driving by subtracting 16 from their age. People who have been driving less than 4 years get charged $1000 a year for insurance, otherwise $600. If a person's age is less than 16 they should not need insurance, so an exception is thrown.

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

public class RateCalc
{

  public static int  calcInsurance( String birth ) throws Exception
  {
    final int year = 2000;
    int age = 0;
    int birthYear = Integer.parseInt( birth );
    age = year -birthYear;

    if ( age < 16 )
    {
      throw new Exception("Age is: " + age );
    }
    else
    {
      int drivenYears = age-16;
      if ( drivenYears < 4 )
        return 1000;
      else
        return 600;
    }
  }

  public static void main ( String[] a ) throws IOException
  {
    BufferedReader stdin = 
        new BufferedReader ( new InputStreamReader( System.in ) );
 
    String  inData = null;
    System.out.println("Enter birth year:");
    inData = stdin.readLine();

    try
    {
      System.out.println( "Your insurance is: " + calcInsurance( inData ) );
    }
    catch ( Exception oops )
    {
      System.out.println( "Too young for insurance!" );
    }

  }
}

The calcInsurance() method constructs an Exception and throws it to its caller, main(), when the bad value is detected The Exception is caught in main() and appropriate action taken.

QUESTION 11:

(Software Design Question: ) Do you think this is a well designed program?

Click Here after you have answered the question