Sie sind auf Seite 1von 7

LAB 6(a) : EXCEPTIONS

LEARNING OUTCOMES
This Labsheet encompasses activities By the end of this lab, students should be able to : 1. 2. 3. Hardware/Software: JCreator with latest version of JDK

ACTIVITY 6A(i) Type the following program. public class Error2 { public static void main(String args[]) { int a=Integer.parseInt(args[0]); int b=Integer.parseInt(args[1]); int c=Integer.parseInt(args[2]); int d=Integer.parseInt(args[3]); double Result= a+b*c/d; System.out.println(The value of the expression is + Result); } }

Procedure : Step 1: Compile and execute the above program. Step 2: Enter positive values for a, b, c and d. See the result. Step 3: Execute the program again and enter the value of d as 0. An arithmetic exception occurs. Step 4: Add try and catch blocks to handle this exception. Step 5: Compile, execute the program and see the result.

ACTIVITY 6A(ii)

Following program will only accept integer value. Procedure : Step 1: Code the following program.

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 ); } }

Step 2: Compile and execute the above program. Step 3: Enter positive values as input. Step 3: Execute the program again and enter the value abc. An arithmetic exception occurs.

ACTIVITY 6A(iii) Following program will illustrate the try and catch block. Procedure : Step 1: Save as the Square class in ACTIVITY 6A(iii) as Square2.java. Change the class name too. Step 2: Add underlined codes (to add try and catch blocks to handle the exception) into the existing program as follow:

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(); try { num = Integer.parseInt( inData ); System.out.println("The square of " + inData + " is " + num*num ); } catch (NumberFormatException ex ) { System.out.println("You entered bad data." ); System.out.println("Run the program again." ); } System.out.println("Completed" ); } }

Step 4:. Compile, execute the program and see the result. Enter 123 as input, state the output produce. Step 5: Execute again your program, enter aaa. Compare the result produce with the one before.

ACTIVITY 6A(iv) Code in Square2 program will be amended to cater user-friendly program. Procedure : Step 1: Save as the Square2 class in ACTIVITY 6A(iv) as Square3.java. Change the class name too. Step 2: Refer to the following codes, make necessary adjustment. Trace it line by line, so that youll face any problems while compiling the program. Be careful on placing the open and close braces for each block (class, main method, while loop, try and catch block)

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 ); } }

Step 4:. Compile, execute the program and see the result. Enter aaa as input. Step 5: Youll be keyed-in the input until the program receive the exact input which is integer number. ACTIVITY 6A(v) Following program will catch the Arithmetic Exception and will demonstrate the finally block. Procedure : Step 1: Code the following program.

import java.io.* ; public class FinallyPractice { 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 (ArithmeticException ex ) { System.out.println("You can't divide " + num + " by " + div); } System.out.println("Good-by" ); } }

Step 2:. Compile, execute the program and see the result. Enter 24 as numerator, and 2 as divisor. State the output. Step 3: Execute again, now enter 24 as numerator and 0 as divisor. State the output produce. Step 4: Add the following code right after the catch block : finally { System.out.println("If the division didn't work, you entered bad data."); } Step 5: Execute again and re-enter the numbers that already entered before to test the program. Step 6: Execute again and enter aaa for both input. What will happen?

Step 7: Add an appropriate codes to overcome the exception based on the problem occur during step 6.

ACTIVITY 6A(vi) In this program we will raise an exception, which will be caught by a catch block. An array of 10 elements is created. Then we will try to assign a value to the 11 th elemen, which raises an exception. The application catches and overrides the exception ArrayIndexOutOfBoundsException

Procedure : Step 1: Code the following program.

import java.io.* ; public class FinallyPractice { public static void main ( String[] a ) throws IOException { int [] arr = new int[10]; try { arr[10]=100; } catch(ArrayIndexOutOfBoundsException e) { e=new ArrayIndexOutOfBoundsException(Hey! You did not notice the length of the array); throw (e); } } }

Step 4:. Compile, execute the program and see the result.

LAB EXERCISES 1. Write a program to get 5 values of array elements from the command line and print those values in the main() method. Add try catch blocks to handle ArrayIndexOutOfBoundsException. Write a program that raises two exceptions. Specify two catch clauses for the exceptions. Each catch block handles a different type of exception. For example exceptions could be ArithmeticException and ArrayIndexOutOfBoundsException Display a message in the finally block.

2.

Das könnte Ihnen auch gefallen