In this program, we will create a method with the "throws" keyword. The throws keyword is used to declare an exception with a method. It informs the programmer that there may occur an exception.
Program/Source Code:
The source code to demonstrate the throws keyword is given below. The given program is compiled and executed successfully.
// Java program to demonstrate throws keyword
public class Main {
public static void division(int num1, int num2) throws ArithmeticException {
//code that may generate ArithmeticException
int res = 0;
res = num1 / num2;
System.out.println("Division is: " + res);
}
public static void main(String[] args) {
try {
division(10, 0);
} catch (Exception e) {
System.out.println("Exception: " + e);
} finally {
System.out.println("The finally block executed");
}
System.out.println("Program Finished");
}
}
Output:
Exception: java.lang.ArithmeticException: / by zero
The finally block executed
Program Finished
In this program, we will create a method with the "throws" keyword. The throws keyword is used to declare an exception with a method. It informs the programmer that there may occur an exception.
Program/Source Code:
The source code to demonstrate the throws keyword is given below. The given program is compiled and executed successfully.
Output: