[go: up one dir, main page]

0% found this document useful (0 votes)
14 views3 pages

Lab 3 (Try, Catch, Finally) Tapil

The document is a Java program demonstrating exception handling for various types of exceptions, including ArithmeticException, IndexOutOfBoundsException, InputMismatchException, NoSuchElementException, and ArrayIndexOutOfBoundsException. Each exception is handled with a try-catch block, providing user feedback for errors encountered during input operations. The program concludes with a message indicating completion and closes the scanner resource.

Uploaded by

kyletap950
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Lab 3 (Try, Catch, Finally) Tapil

The document is a Java program demonstrating exception handling for various types of exceptions, including ArithmeticException, IndexOutOfBoundsException, InputMismatchException, NoSuchElementException, and ArrayIndexOutOfBoundsException. Each exception is handled with a try-catch block, providing user feedback for errors encountered during input operations. The program concludes with a message indicating completion and closes the scanner resource.

Uploaded by

kyletap950
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tapil, Kyle Jefferson E.

BSIT-A201

Code:
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class ExceptionHandling {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// ArithmeticException
try {
System.out.print("Enter numerator: ");
int num = scanner.nextInt();
System.out.print("Enter denominator: ");
int denom = scanner.nextInt();
System.out.println("Result: " + (num / denom));
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");
} finally {
System.out.println("Arithmetic operation attempt completed.");
}

// IndexOutOfBoundsException
try {
int[] numbers = {1, 2, 3};
System.out.print("Enter an index (0-2): ");
int index = scanner.nextInt();
System.out.println("Array value: " + numbers[index]);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: The specified index is out of bounds.");
} finally {
System.out.println("Array access attempt completed.");
}

// InputMismatchException
try {
System.out.print("Enter an integer: ");
int value = scanner.nextInt();
System.out.println("You entered: " + value);
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. Please enter a valid
integer.");
scanner.next();
} finally {
System.out.println("Input handling completed.");
}

// NoSuchElementException
try {
scanner.nextLine();
System.out.print("Enter a word: ");
String text = scanner.next();
System.out.println("You entered: " + text);
} catch (NoSuchElementException e) {
System.out.println("Error: No input detected. Please provide a valid
input.");
}

// ArrayIndexOutOfBoundsException
try {
int[] array = new int[3];
array[5] = 10;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Array index is out of range.");
}

System.out.println("Program completed.");
scanner.close();
}
}

Sample Invalid input:


• Arithmetic Exception

• Index out of bounds exception


• Input mismatch exception

You might also like