28 lines
805 B
Java
28 lines
805 B
Java
class MyException extends Exception {
|
|
public MyException(String message) {
|
|
super(message);
|
|
}
|
|
}
|
|
|
|
public class P5 {
|
|
public static void main(String[] args) {
|
|
try {
|
|
int result = divide(10, 0);
|
|
System.out.println("Result: " + result);
|
|
} catch (MyException e) {
|
|
System.out.println("My Exception: " + e.getMessage());
|
|
} catch (ArithmeticException e) {
|
|
System.out.println("Arithmetic Exception: " + e.getMessage());
|
|
} finally {
|
|
System.out.println("Finally block executed.");
|
|
}
|
|
}
|
|
|
|
public static int divide(int num1, int num2) throws MyException {
|
|
if (num2 == 0) {
|
|
throw new MyException("Division by zero not allowed.");
|
|
}
|
|
return num1 / num2;
|
|
}
|
|
}
|