Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-exception Example to show Argument Exception in java

Related Tutorials


 
 

Share on Google+Share on Google+

Example to show Argument Exception in java

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

Example to show Argument Exception in java

     

In this Tutorial  we explain an example from Argument exception in java .Exceptions are the condition  in Java to indicate a calling method that an abnormal condition has occurred. This tutorial describes  how to handle Argument exceptions appropriately in your programs. The steps involved in the program are described below:-

private static Scanner key = new Scanner(System.in):-Here we are declaring Scanner class object. Scanner is a class which is used to parse primitive types and strings using regular expressions.

Input = key. next():-This is the method by which we can find and returns the next complete token from this scanner.

System.out.println(Input + " is not an Integer value" + "\n"+ "Please enter appropriate value"):-This is the Exception message which is to be displayed if the number you entered is not a real number.

ArgumentException.java


import java.util.*;

public class ArgumentException {

  private static Scanner key = new Scanner(System.in);

  public static void main(String args[]) {
  int index;
  int i;
  String Input;

  for (index = 0; index < args.length; index++) {
  }

  System.out.print("Please enter a real number : ");
  Input = key.next();
  try {
  i = Integer.parseInt(Input);
  System.out.println("The value is " + i);
  catch (NumberFormatException e) {
  System.out.println(Input + " is not an Integer value" "\n" 
  "Please enter apporipriate value");
  }
  }
}

Output of the program

Please enter a real number : Girish
Girish is not an Integer value
Please enter apporipriate value

Download sourecode

 

Advertisements

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

Discuss: Example to show Argument Exception in java  

Post your Comment


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