Q:

Java program to demonstrate the throws keyword

belongs to collection: Java Exception Handling Programs

0

Java program to demonstrate the throws keyword

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java program to implement nested try catch block... >>
<< Java program to throw an exception explicitly...