Home Java Java-exception Example to show exception in java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Example to show exception in java

Advertisement
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

     

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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 22, 2008

Related Tutorials

Ask Questions?    Discuss: Example to show exception in java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com