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

Lab5 Vivaan 81

The document contains six Java programs that demonstrate exception handling for various scenarios, including division by zero, array index out of bounds, invalid input, null pointer exceptions, and input mismatch exceptions. Each program prompts the user for input and uses try-catch blocks to handle potential errors gracefully. The programs illustrate the importance of validating user input and managing exceptions to prevent crashes.

Uploaded by

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

Lab5 Vivaan 81

The document contains six Java programs that demonstrate exception handling for various scenarios, including division by zero, array index out of bounds, invalid input, null pointer exceptions, and input mismatch exceptions. Each program prompts the user for input and uses try-catch blocks to handle potential errors gracefully. The programs illustrate the importance of validating user input and managing exceptions to prevent crashes.

Uploaded by

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

1.import java.util.

Scanner;

public class Lab51 {


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

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


double numerator = scanner.nextDouble();

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


double denominator = scanner.nextDouble();

try {
if (denominator == 0) {
throw new ArithmeticException("Denominator cannot be zero.");
}

double result = numerator / denominator;


System.out.println("The result of division is: " + result);

} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
}
}

2.import java.util.Scanner;
public class Lab52 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

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

System.out.print("Enter the index to access: ");


int index = scanner.nextInt();

try {
System.out.println("Element at index " + index + ": " + array[index]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Invalid index. " + e.getMessage());
}
}
}

3.import java.util.Scanner;

public class Lab53 {


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

try {
System.out.print("Enter the first number: ");
String input1 = scanner.nextLine();
int num1 = Integer.parseInt(input1);

System.out.print("Enter the second number: ");


String input2 = scanner.nextLine();
int num2 = Integer.parseInt(input2);

int result = num1 / num2;


System.out.println("The result of division is: " + result);

} catch (NumberFormatException e) {
System.out.println("Error: Invalid input. Please enter valid integers.");
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");
}
}
}

4.public class Lab54 {


public static void main(String[] args) {
String str = null;

try {
System.out.println("Length of the string: " + str.length());
} catch (NullPointerException e) {
System.out.println("Error: NullPointerException - Cannot call method on a null object.");
}
}
}

5.import java.util.Scanner;
import java.util.InputMismatchException;

public class Lab55 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
System.out.println("You entered: " + number);
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. Please enter a valid integer.");
}
}
}

6.import java.util.Scanner;

public class Lab56 {


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

try {
System.out.print("Enter your age: ");
int age = scanner.nextInt();

if (age < 0 || age > 120) {


throw new IllegalArgumentException("Invalid age: Age must be between 0 and 120.");
}

System.out.println("Your age is: " + age);

} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}
}
}

You might also like