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.
IllegalArgumentException is a Runtime Exception.
Class Hierarchy is as follows:
- java.lang.Object
- java.lang.Throwable
- java.lang.Exception
- java.lang.RuntimeException
- java.lang.IllegalArgumentException
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