Week 4 KCPD
Week 4 KCPD
class Demo {
public static void method1() throws IOException {
throw new IOException("IO exception occurred");
}
public static void method2() throws ArithmeticException {
throw new ArithmeticException("Arithmetic exception occurred");
}
public static void method3() throws IOException, ArithmeticException {
method1();
method2();
}
public static void method4() throws IOException {
try (FileWriter fw = new FileWriter("output.txt")) {
fw.write("Hello world");
}
}
public static void main(String[] args) {
try {
method4();
System.out.println("File written successfully");
} catch (IOException e) {
System.out.println("IO exception: " + e.getMessage());
}
try {
method3();
} catch (IOException | ArithmeticException e) {
System.out.println("Exception: " + e.getMessage());
}
}
}