Finally- exception handling
import java.util.Scanner;
public class nineteen {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int result = divideByTwo(num);
System.out.println("Result of dividing by 2: " + result);
}
catch (ArithmeticException e) {
System.out.println("Exception: " + e.getMessage());
}
finally {
System.out.println("Finally block is executed, regardless of whether an exception occurred
or not.");
scanner.close(); // Close the Scanner to release resources
}
}
public static int divideByTwo(int num) {
if (num % 2 != 0) {
throw new ArithmeticException("Number must be even for this operation.");
}
return num / 2;
}
}