Example to show exception in java

Here we are describing the use of using exception class in java .Exceptions are the way in Java to indicate to a calling method that an abnormal condition has occurred.

Example to show exception in java

Here we are describing the use of using exception class in java .Exceptions are the way in Java to indicate to a calling method that an abnormal condition has occurred.

Example to show exception in java

Example to show exception in java

     

The code describe you  the use of exception class in java .Exceptions are the set of condition in Java to indicate to a calling method that occurred when an abnormal condition occurred. This tutorial describes  how to use exceptions appropriately in your programs.. The following steps involved in the program are given below:-

division(100, 0):- This method is declared for calculating the average of any two numbers. Here 100 is the numerator and 0 is the denominator.

System.out.println("Divide" + ex.getMessage() + "Exception" + "\n"  + "Please check the denominator");:- This return you an  exception message which will be displayed if an error occurs.  

Exceptionjava.java


public class Exceptionjava {

  public static void main(String[] args) {
  division(1000);
  }
  public static void division(int numerator, int denominator) {
  try {
  int average = numerator / denominator;
  System.out.println("Average : " + average);
  catch (Exception ex) {
  System.out.println("Divide" + ex.getMessage() "Exception" "\n" 
  "Please check the denominator");
  }
  }
}

output of the program

Divide/ by zero Exception
Please check the denominator

Note:- if you will provide the value of denominator as 100 or any number except 1 it will not give any exceptions.

Download source code