0% found this document useful (0 votes)
10 views1 page

Exception Handling

Uploaded by

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

Exception Handling

Uploaded by

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

import java.io.

BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ExceptionHandling {


public static void main(String[] args) {
try {
int result = divide(10, 0);
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Caught ArithmeticException: " + e.getMessage());
} finally {
System.out.println("Finished Arithmetic Exception Handling.");
}

try {
readFile("nonexistentfile.txt");
} catch (IOException e) {
System.out.println("Caught IOException: " + e.getMessage());
} finally {
System.out.println("Finished IOException Handling.");
}
}

public static int divide(int a, int b) {


if (b == 0) {
throw new ArithmeticException("Division by zero is not allowed.");
}
return a / b;
}

public static void readFile(String fileName) throws IOException {


BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
System.out.println("File content: " + line);
reader.close();
}
}

You might also like