[go: up one dir, main page]

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

Kushal Cse3c 184

fvwfvefvq

Uploaded by

prokhandelwal21
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)
2 views3 pages

Kushal Cse3c 184

fvwfvefvq

Uploaded by

prokhandelwal21
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

Q1:

Write a program in JAVA to evaluate the expression z = 65/(x-y) and handle


the exception in such a way that your program will not be aborted even if
x= y.
Code:
import java.util.*;
class lab {
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
System.out.print("Enter value for x: ");
int x = ob.nextInt();
System.out.print("Enter value for y: ");
int y = ob.nextInt();
try {
double z = 65 / (x - y);
System.out.println("The value of z is: " + z);
} catch (ArithmeticException e) {

System.out.println("Error Message: Division by zero is not allowed(i.e.


x=y is not allowed)");
}
}
}

output:
Q2:
Write a program in JAVA to read an integer array of size 10 and also
display the element at the 12th index position of the array. Handle the
exception in such a way that your program will not abort.
Code:
import java.util.*;
public class pos{
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
int[] arr = new int[10];
System.out.println("Enter 10 integers:");
for (int i = 0; i <10; i++) {
arr[i] = ob.nextInt();
}
try {
System.out.println("Element at index 12: " + arr[12]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error Message: Index 12 is out of bounds for an array with
size=10");
}
}
}
Output:

You might also like