JAVA ASSIGNMENT
JAVA ASSIGNMENT
Roll no-2200290530024
Sec- A
JAVA ASSIGNMENT-02
1). WAP to create a method that reads a file and throws an exception if
the file
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
try {
String line;
System.out.println("File content:");
System.out.println(line);
} catch (IOException e) {
throw new Exception("An error occurred while reading the file: " + e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
throw new Exception("An error occurred while closing the file: " + e.getMessage());
Name-Aryan
Roll no-2200290530024
Sec- A
try {
readFile(filePath);
} catch (Exception e) {
System.err.println(e.getMessage());
import java.util.Scanner;
Roll no-2200290530024
Sec- A
if (scanner.hasNextInt()) {
array[i] = scanner.nextInt();
} else {
System.out.println("Array elements:");
scanner.close();