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

Java Lab 11

The program prompts the user to enter two numbers and handles potential exceptions. It catches NumberFormatException if non-integers are entered, ArithmeticException if the second number is zero, and displays the exception message. The program then defines an array, prompts the user for an index, and handles NumberFormatException if a non-integer is entered and ArrayIndexOutOfBoundsException if the index is invalid, displaying error messages.

Uploaded by

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

Java Lab 11

The program prompts the user to enter two numbers and handles potential exceptions. It catches NumberFormatException if non-integers are entered, ArithmeticException if the second number is zero, and displays the exception message. The program then defines an array, prompts the user for an index, and handles NumberFormatException if a non-integer is entered and ArrayIndexOutOfBoundsException if the index is invalid, displaying error messages.

Uploaded by

Kaustav Lahiri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1. Implement a program to handle Arithmetic exception.

The user enters two numbers


Num1 and Num2. The division of Num1 and Num2 is displayed. If Num1 and Num2 are
not integers, the program would throw a Number Format Exception. If Num2 were zero,
the program would throw an Arithmetic Exception. Display the exception.
[CO: PCC-CS593.4]

import java.util.Scanner;

public class ArithmeticExceptionHandling {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

try {
// Get user input for Num1
System.out.print("Enter Num1: ");
int num1 = Integer.parseInt(scanner.nextLine());

// Get user input for Num2


System.out.print("Enter Num2: ");
int num2 = Integer.parseInt(scanner.nextLine());

// Perform division and display the result


int result = divide(num1, num2);
System.out.println("Result of division: " + result);

} catch (NumberFormatException e) {
// Handle NumberFormatException
System.out.println("NumberFormatException: Please enter valid integers.");

} catch (ArithmeticException e) {
// Handle ArithmeticException
System.out.println("ArithmeticException: Division by zero is not allowed.");

} finally {
// Close the scanner
scanner.close();
}
}

// Method to perform division and handle ArithmeticException


private static int divide(int num1, int num2) {
if (num2 == 0) {
throw new ArithmeticException("Division by zero is not allowed.");
}
return num1 / num2;
}
}
Q2. The program will use a predefined array of integers. The user will be prompted to enter
an index to access an element from the array. The program should handle potential
exceptions gracefully, such as when the user enters a non-integer value for the index or
when the index is out of bounds for the predefined array.
Program Requirements:
1. Define a predefined array of integers (e.g., {10, 20, 30, 40, 50}).
2. Prompt the user to enter an index to access an element from the array.
3. Use try-catch blocks to handle potential exceptions:
 If the user enters a non-integer value for the index, catch and handle
NumberFormatException.
 If the user enters an index that is out of bounds for the predefined array,
catch and handle ArrayIndexOutOfBoundsException.
 Handle any other unexpected exceptions and display appropriate error
messages.
4. Display the value of the array element at the specified index if no exceptions occur.
5. Ensure proper resource management, such as closing any opened resources.
[CO: PCC-CS593.4]

import java.util.Scanner;

public class SimpleArrayExceptionHandling {

public static void main(String[] args) {


// Predefined array
int[] numbers = {10, 20, 30, 40, 50};

Scanner scanner = new Scanner(System.in);

try {
// Get user input for the index to access
System.out.print("Enter the index to access (0 to " + (numbers.length - 1) + "): ");
int index = Integer.parseInt(scanner.nextLine());

// Attempt to access an element at the specified index


int value = numbers[index];

// Display the value


System.out.println("Value at index " + index + ": " + value);

} catch (NumberFormatException e) {
// Handle NumberFormatException
System.out.println("NumberFormatException: Please enter a valid integer.");

} catch (ArrayIndexOutOfBoundsException e) {
// Handle ArrayIndexOutOfBoundsException
System.out.println("ArrayIndexOutOfBoundsException: Index is out of bounds.");

} catch (Exception e) {
// Handle other exceptions
System.out.println("Exception: " + e.getMessage());
} finally {
// Close the scanner
scanner.close();
}
}
}

You might also like