package exceptions;
public class Employee {
int age;
int empId;
String empName;
int getAge(){
return age;
}
int getEmpId(){
return empId;
}
String getEmpName(){
return empName;
}
void setAge(int age) throws AgeMisMatchException
{
if(age<18)
throw new AgeMisMatchException("Age must be greater than 18");
else
{
this.age=age;
}
}
void setEmpId(int empId){
this.empId=empId;
}
void setEmpName(String empName){
this.empName=empName;
}
}
public class Employee {
int age;
int empId;
String empName;
int getAge(){
return age;
}
int getEmpId(){
return empId;
}
String getEmpName(){
return empName;
}
void setAge(int age) throws AgeMisMatchException
{
if(age<18)
throw new AgeMisMatchException("Age must be greater than 18");
else
{
this.age=age;
}
}
void setEmpId(int empId){
this.empId=empId;
}
void setEmpName(String empName){
this.empName=empName;
}
}
User Defined Exception
package exceptions;
public class AgeMisMatchException extends Exception{
AgeMisMatchException(){
}
AgeMisMatchException(String message) {
super(message);
}
}
package exceptions;
import java.util.*;
public class EmployeeDemo {
public void addEmployee()
{
try
{
Scanner s=new Scanner(System.in);
Employee emp=new Employee();
System.out.println("Enter employee age ");
int age=s.nextInt();
emp.setAge(age);
System.out.println("Enter employee empid ");
int empId=s.nextInt();
emp.setEmpId(empId);
System.out.println("Enter employee Name ");
String empName=s.next();
emp.setEmpName(empName);
System.out.println("Employee Age "+emp.getAge());
System.out.println("Employee Id "+emp.getEmpId());
System.out.println("Employee Name "+emp.getEmpName());
}catch(AgeMisMatchException e)
{
System.out.println(e.getMessage());
}
}
public static void main(String[] args) throws AgeMisMatchException{
EmployeeDemo e=new EmployeeDemo();
e.addEmployee();
}
}
0 comments:
Post a Comment