Upload
vamsirathan
View
18
Download
2
Embed Size (px)
DESCRIPTION
exceptions
Citation preview
package exceptionseg1;
public class AgeMisMatchException extends Exception{
public AgeMisMatchException(){// TODO Auto-generated constructor stub}
public AgeMisMatchException(String msg){
super(msg);// TODO Auto-generated constructor stub
}
}
package exceptionseg1;
public class Employee {private int age;private int empId;private String empName;
/*public Employee(int age, int empId, String empName) {this.age=age;this.empId=empId;this.empName=empName;// TODO Auto-generated constructor stub
}*/
public int getAge(){
return age;}
public int getEmpId(){
return empId;}
public String getEmpName(){
return empName;}
public void setAge(int age){
this.age=age;}
public void setEmpId(int empId){
this.empId=empId;}
public void setEmpName(String empName){
this.empName=empName;}
}
package exceptionseg1;
import java.util.Scanner;
public class EmpRegistration extends Employee {
public static void main(String[] args) throws AgeMisMatchException
{//Employee e=new Employee();EmpRegistration er=new EmpRegistration();
er.addEmployee();System.out.println("END");
}/*try{
if(e.getAge()<18){
System.out.println(e.getAge());throw new AgeMisMatchException("age less than
18");}
}catch(AgeMisMatchException exp){
System.out.println(exp.getMessage());}System.out.println("END");
}*/public void addEmployee() throws AgeMisMatchException{
Employee e=new Employee();
Scanner sc= new Scanner(System.in);System.out.println("Enter age:");int age=sc.nextInt();
if(age!=0 && age<18){ throw new AgeMisMatchException("age less than 18");}else{e.setAge(age);}
System.out.println("Enter ID:");int empId=sc.nextInt();e.setEmpId(empId);
Scanner sc1= new Scanner(System.in);System.out.println("Enter EMPName:");String empName=sc1.nextLine();e.setEmpName(empName);
//sc.close();//sc1.close();
}
}package exceptionseg1;
public class ExcepTest {
public static void main(String[] args){
// TODO Auto-generated method stubtry{
int a[]=new int[2];System.out.println("Access element three:"+a[3]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception thrown :"+e);}System.out.println("Out of the block");
}}
package excepeg2;
import java.util.Scanner;
public class Division {
public static void main(String[] args){
Division d=new Division();// TODO Auto-generated method stubtry{
Scanner sc= new Scanner(System.in);System.out.println("Enter a:");int a=sc.nextInt();System.out.println("Enter b:");int b=sc.nextInt();double res=d.division(a,b);
if(res!=0)throw new ArithmeticException("Not divisible");
}catch(ArithmeticException exp){
System.out.println(exp.getMessage());}System.out.println("END");
}
public double division(int a,int b) {
// TODO Auto-generated constructor stubreturn a%b;
}
}
--------------------------package excepeg2;
class AgeException extends Exception {
}
package excepeg2;
public class Person
{private int age=25;public void setAge(int val){
age=val;}public int getAge(){
return age;}
}
package excepeg2;
public class SupplyPerson{
public static void main(String args[]){
int age=0;if(args.length>0){
try{
age=Integer.parseInt(args[0]);}catch(NumberFormatException e){
System.out.println("Must enter integer as first argument.");
return;}
}else{
System.out.println("Must enter age as first argument..");
return;}
Person p=new Person();p.setAge(age);
VirtualJob j=new VirtualJob();VirtualCompany.fillJob(j,p);
}
}
package excepeg2;
class TooOldException extends AgeException {
}
package excepeg2;
class TooYoungException extends AgeException {
}
package excepeg2;
public class VirtualCompany {
public static void fillJob(VirtualJob j,Person p){
try{
j.applyJob(p);System.out.println("Age is right.");
}catch(AgeException e){
System.out.println("Age is tooOld or tooYoung");}
}}
package excepeg2;
public class VirtualJob{
private static final int tooOld=35;private static final int tooYoung=15;public void applyJob(Person p) throws
TooOldException,TooYoungException{
int age=p.getAge();if(age>= tooOld){
throw new TooOldException();}
else if (age<=tooYoung){
throw new TooYoungException();}
}}