Thursday, November 24, 2011

IllegalArgumentException


packageorg.training.exceptions;

public class IllegalArgument {

      public static void main(String[] args) {
           
            IllegalArgument ia = new IllegalArgument();
            try {
                  ia.simpleMethod("Hello");
                  ia.simpleMethod(null);
            }catch(IllegalArgumentException iae) {
                  System.err.println("Illegal Argument Exception has been caught, check your arguments again");
                  iae.printStackTrace();
            }
      }
     
      public voidsimpleMethod(String argument) {
            if(argument == null) {
                  throw newIllegalArgumentException();
            }else
                  System.out.println(argument);
      }

}


Output:
Hello
Illegal Argument Exception has been caught, check your arguments again
java.lang.IllegalArgumentException
      at org.training.exceptions.IllegalArgument.simpleMethod(IllegalArgument.java:19)
      at org.training.exceptions.IllegalArgument.main(IllegalArgument.java:10)

No comments:

Post a Comment