Exercise 7 w/ Solution

16/10/2009 09:33

Exercise Objective – In this exercise, you will use the try-catch block to handle a simple runtime exception.
Tasks
The TestExceptions program prints out all of the command-line arguments, but it has a fatal flaw: It will
eventually attempt to access beyond the args array bounds.
public class TestExceptions {
public static void main(String [ ] args) {
for ( int i = 0; true; i++) {
System.out.println(“args[“ + I + “] is ‘” + args[i] + “ ’ ”);
}
}
}
Note – The code in this program is poorly implemented, because the loop should test for the exact size of the
args array. However, the purpose of this exercise it to give you hands-on experience with exceptions and how to
catch them.
1. Compile and run the TestExceptions program.
2. Modify the TestExceptions program to handle the ArrayIndexOutOfBoundsException by wrapping the
for loop in a try block.
3. Write a catch block that catches that exception and prints out the exception object and then prints that
the application is quitting.
4. Re-compile and run the program.
The output should look like this:
> javac TestExceptions.java
> java TestExceptions one two three
args[0] is ‘one’
args[1] is ‘two’
args[2] is ‘three’
Exception caught: java.lang.ArrayIndexOutOfBoundsException
Quitting…
Here is a possibly incorrect program:
import java.util.* ;
public class DivisionPractice
{
public static void main ( String[] a )
{
Scanner scan = new Scanner( System.in );
int num=0, div=0 ;
try
{
System.out.print("Enter the numerator: ");
num = scan.nextInt();
System.out.print("Enter the divisor : ");
div = scan.nextInt();
System.out.println( num + " / " + div + " is " + (num/div) + " rem " + (num%div) );
}
catch (InputMismatchException 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);
}
}
}
Enhance the DivisionPractice program:
1. Put in a loop so that the user is repeatedly asked for the numerator and the divisor. For each set of data,
the program prints out the result, or an informative error message if there is a problem (division by zero
or poor input data).
2. The program contines looping, even if there is a problem
3. Exit the loop when data entered for the numerator start with characters "q" or "Q". Don't print out an
error message in this case.
4. Don't ask for the divisor if the user just asked to quit.
Here is sample output from one run:
Enter the numerator: 12
Enter the divisor: 4
12 / 4 is 3
Enter the numerator: 12
Enter the divisor : 0
You can't divide 12 by 0
Enter the numerator: glarch
You entered bad data.
Please try again.
Enter the numerator: quit
You will need to use the method charAt() from the String class.

 

Solution for first Problem:

public class TestExceptions {
public static void main(String [ ] args) {

 try {
       for ( int i = 0; true; i++)

    }
catch (ArrayIndexOutOfBoundsException enf) {
      System.out.println(“args[“ + i + “] is ‘” + args[i] + “ ’ ”);
    System.out.println("Exception caught:" + "java.lang.ArrayIndexOutOfBoundsException
Quitting…");
    }



  }
}

Solution for second Problem:

import java.util.* ;
public class DivisionPractice
{
public static void main ( String[] arg )
{
Scanner scan = new Scanner( System.in );
int num=0, div=0 ;

do
{
try
{
System.out.print("Enter the numerator: ");
num = scan.nextInt();

if (num=='q')||num=='Q'){
sytem.exit(0);
}
else
    {
System.out.print("Enter the divisor : ");
div = scan.nextInt();
System.out.println( num + " / " + div + " is " + (num/div) + " rem " + (num%div) );
    }
}
catch (InputMismatchException 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);
}

} while(num!='q')||num!='Q');

}
}

Back