Tuesday, 16 September 2014

JAVA EXCEPTION - IllegalArgumentException

Java Exception -> IllegalArgumentException

IllegalArgumentException is a Runtime Exception.

Class Hierarchy is as follows: 






 IllegalArgumentException thrown to indicate that a method has been passed an Illegal Argument.


Example:

public class Voters {

 String voter_name;
 int voter_age;


//u can throw this exception like this
//rule: age should be > 18 years

 private void checkAge(int voter_age){
     if(voter_age<18)
     {
      throw new IllegalArgumentException();
     }
 }
 //constructor
 public Voters(String voterName,
                 String voterAge) {
   checkAge(voterName,voterAge);
   voter_name=voterName;
   voter_age=voterAge;
 }

           .
           .
           .

  Like this you can throw this exception.



No comments:

Post a Comment