User Defined Exception
package exceptions;
public class TooYoungException extends Exception {
private static final long serialVersionUID = 1L;
public TooYoungException(String msg)
{
super(msg);
}
}
package exceptions;
public class TooYoungException extends Exception {
private static final long serialVersionUID = 1L;
public TooYoungException(String msg)
{
super(msg);
}
}
User Defined Exception
package exceptions;
public class TooOldException extends Exception{
private static final long serialVersionUID = 1L;
public TooOldException(String msg)
{
super(msg);
}
}
package exceptions;
import java.util.*;
public class VirtualJob {
static final int tooOld=0;
static final int tooYoung=0;
void applyJob(Person p)throws TooOldException,TooYoungException{
Scanner s=new Scanner(System.in);
System.out.println("enter name ");
String name=s.next();
p.setName(name);
System.out.println("enter age ");
int age=s.nextInt();
p.setAge(age);
}
}
package exceptions;
public class VirtualCompany {
public static void fillJob(VirtualJob vj,Person p)throws TooOldException,TooYoungException{
vj.applyJob(p);
}
}
package exceptions;
public class Person {
int age;
String name;
void setAge(int age)throws TooYoungException,TooOldException{
if(age<22)
throw new TooYoungException("too young for applying job ");
else if(age>35)
throw new TooOldException("too old for applying job ");
else
this.age=age;
}
void setName(String name){
this.name=name;
}
public int getAge(){
return age;
}
public String getName(){
return name;
}
}
package exceptions;
public class AgeDemo {
public static void main(String[] args) {
Person p=new Person();
VirtualJob vj=new VirtualJob();
try
{
vj.applyJob(p);
VirtualCompany.fillJob(vj, p);
}catch(TooYoungException ty)
{
System.out.println(ty.getMessage());
}
catch(TooOldException to)
{
System.out.println(to.getMessage());
}
}
}
0 comments:
Post a Comment