0% found this document useful (0 votes)
3 views2 pages

Lab Evaluation 05

The document contains three Java programs that demonstrate exception handling. Problem 1 handles division by zero, Problem 2 manages array index out of bounds, and Problem 3 addresses both division by zero and invalid number format while also accessing an array. Each program uses a try-catch block to manage potential runtime errors and provides user prompts for input.

Uploaded by

Mahin
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)
3 views2 pages

Lab Evaluation 05

The document contains three Java programs that demonstrate exception handling. Problem 1 handles division by zero, Problem 2 manages array index out of bounds, and Problem 3 addresses both division by zero and invalid number format while also accessing an array. Each program uses a try-catch block to manage potential runtime errors and provides user prompts for input.

Uploaded by

Mahin
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/ 2

Problem 1:

import java.util.Scanner;

public class Problem1 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter numerator: ");

int a = scanner.nextInt();

System.out.print("Enter denominator: ");

int b = scanner.nextInt();

try {

int result = a / b;

System.out.println("Result: " + result);

} catch (ArithmeticException e) {

System.out.println("Division by zero is not allowed.");

scanner.close();

Problem 2:
import java.util.Scanner;

public class Problem2 {


public static void main(String[] args) {
int[] array = {1, 2, 3, 4};
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an index: ");
int index = scanner.nextInt();

try {
System.out.println("Element at index " + index + " is " + array[index]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index out of bounds.");
}

scanner.close();
}
}
Problem 3:
import java.util.Scanner;

public class Problem3 {


public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40};
Scanner scanner = new Scanner(System.in);

try {
System.out.print("Enter a number to divide 100 by: ");
int divisor = scanner.nextInt();
int result = 100 / divisor;
System.out.println("Division result: " + result);

System.out.print("Enter an index to access from array: ");


int index = scanner.nextInt();
System.out.println("Element at index " + index + ": " + numbers[index]);

System.out.print("Enter a number as a string: ");


scanner.nextLine(); // consume leftover newline
String str = scanner.nextLine();
int parsed = Integer.parseInt(str);
System.out.println("Parsed integer: " + parsed);

} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Array index out of bounds.");
} catch (NumberFormatException e) {
System.out.println("Error: Invalid number format.");
}

scanner.close();
}
}

You might also like