0% found this document useful (0 votes)
5 views

Lab6-ExceptionHandling 1

Uploaded by

ongspeed123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab6-ExceptionHandling 1

Uploaded by

ongspeed123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Student D:6630614003 Name: Supasin Anafarang .

Major:AISE

Lab 6 Exception Handling


Objectives:
• Identify types of Java exception correctly
• Write Java program with statements to handle each type of exceptions
correctly
Instructions:
• Answer the question.
• Capture screen of your code in IntelliJ with output, paste in this file. Save in
pdf file and submit to the assignment in MS Teams.

1. Test running program and enter inputs in the following test cases below.

import java.util.Scanner;

public class ExceptionDemo {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a:");
int a = sc.nextInt();
System.out.print(a);
System.out.print("Enter b:");
int b = sc.nextInt();
System.out.print(b);
System.out.println("a/b = " + (a / b));
System.out.println("a*b = "+ (a*b));
System.out.print("End of program.");
}
}

Test case a b Result


1 5 2 2 , 10
2 a Exception in thread "main"
java.util.InputMismatchException
3 5.5 Exception in thread "main"
java.util.InputMismatchException
4 5 0 Exception in thread "main"
java.lang.ArithmeticException: /
by zero

Insert code using try-catch for each exception type. Test running program for each test
case. The program must print “End of program.” even an exception occurs.

Answer: (Code + output)

1
2
3
2. Test running the code below.
import java.util.Scanner;

public class ExceptionDemo {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = {4, 3, 8, 10, 22, 40, 12, 55, 60, 88, 7};
System.out.println("Searching data in an array....");
System.out.print("Enter index of the element:");
int index = sc.nextInt();

for (int i=0; i<arr.length; i++) {


System.out.println(arr[i] + “ “ + (arr[i]/i));
}
System.out.print("End of program.");
}
}

What is the result in each test case? Answer:

Test case Input Result


1 6 Exception in thread "main"
java.lang.ArithmeticException: / by zero
2 2.5 Exception in thread "main"
java.util.InputMismatchException
3 0 Exception in thread "main"
java.lang.ArithmeticException: / by zero
4 X Exception in thread "main"
java.util.InputMismatchException
5 20 Exception in thread "main"
java.lang.ArithmeticException: / by zero

Insert the try-catch statement, separate catch statements according to the type of
exception to handle all possible the exceptions by showing exception name.

Answer:

4
5
6
3. From the code with throw statements. What statements cause the program
compile error? Why?
Answer: Unhandled exception: java.lang.Exception

What is the result in


each test case? Answer:
import java.util.Scanner;
input Result
public class ThrowDemo {
public static void main(String[] args) {
1 1st case
Scanner sc = new Scanner(System.in); End of program.
System.out.print("Enter number (1-4) :");
int n = sc.nextInt(); 2 Exception in thread "main"
switch(n) { java.lang.RuntimeException:
case 1: System.out.println("1st case"); RuntimeException!
break;
case 2: System.out.println("2nd case"); 3 Exception in thread "main"
case 3: System.out.println("3rd case"); java.lang.RuntimeException:
throw new RuntimeException!
RuntimeException("RuntimeException!"); 4 Exception in thread "main"
case 4: System.out.println("4th case"); java.lang.Exception: Exception
throw new Exception("Exception!");
} 9 End of program
System.out.print("End of program."); T Exception in thread "main"
java.util.InputMismatchException
}
}

3.1 Write try-catch to handle the exception.


3.2 What is the output when you enter 1, 2, 3, 4, 9, T. What exceptions can occur
during the program is running and cause of termination?
3.3 Add the code in swith-case block to check the input, if the value is not 1-4,
throws the InputMismatchException object. Write the program to handle this
exception with try-catch and show the cause of exception.

Answer:

7
8
4. The class FileManagement is declared below. Write a Java program. In the
main method of your program, create an object of FileManagement class and
run program. What is the exception reported? Add the code to handle with that
exception.

import java.io.FileReader;
import java.io.IOException;

public class FileManagement {

9
FileReader reader = null;

public void openFile() throws IOException {


reader = new FileReader("somefile.txt");
int i = 0;
while (i != -1) {
i = reader.read();
System.out.print((char) i);
}
}
}

Answer:

10
11

You might also like