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

Lab Task 05.Docx

Uploaded by

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

Lab Task 05.Docx

Uploaded by

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

Problem 1

Write a program that takes two integers as input and performs division. Use a try-catch block to handle
the ArithmeticException that occurs when dividing by zero. Display an appropriate message if the
exception is caught.

Expected Behavior:

● Input: 10, 2 → Output: 5

● Input: 10, 0 → Output: "Division by zero is not allowed."

Problem 2
Create a program that initializes an array of integers. Take an index as input from the user and use a
try-catch block to catch ArrayIndexOutOfBoundsException if the user tries to access an invalid index.
Print the value at the index if valid, or an error message if the index is out of bounds.

Expected Behavior:

● Input: Array = {1, 2, 3, 4}, Index = 2 → Output: "Element at index 2 is 3"

● Input: Array = {1, 2, 3, 4}, Index = 5 → Output: "Index out of bounds."

Problem 3
Write a program that performs multiple operations inside a try block. These operations include:

● Reading an integer from the user and dividing it by another number.

● Accessing an element from an array.

● Parsing a string to an integer.

Handle multiple exceptions, including ArithmeticException, ArrayIndexOutOfBoundsException, and


NumberFormatException, using separate catch blocks.

Expected Behavior:

● Input: Valid values → Outputs result correctly.

● Input: Division by zero, out-of-bound index, or invalid string → Catches and handles each
exception appropriately.

You might also like