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

|