Friday, 27 November 2015

Exception Programs..


Create a class USERTRAIL with following specifications.
                        val1, val2 type    int
Method boolean show () will check if val1 and val2 are greater or less than Zero Have constructor which will val1, val2 and check whether if it is less than 0  then raise a custom Exception (name: Illegal value exception.)

package uni;

class Usertrail {
    int val1;
    int val2;
    int getVal1() {
        return val1;
    }
    void setVal1(int val1) {
        this.val1 = val1;
    }
    int getVal2() {
        return val2;
    }
    void setVal2(int val2) {
        this.val2 = val2;
    }
     void booleanshow(){
     }

    public Usertrail(int val1, int val2) {
         if(val1 < 0 || val2 < 0)
         {
             try{
                 throw new IllegalValueException();
             }catch(IllegalValueException e)
             {
                 System.out.println(e);
             }
         }
   
        this.val1 = val1;
        this.val2 = val2;
    }
    void display()
    {
        System.out.println("Number is "+val1+"\n Number is "+val2);
    }
    public static void main(String[] args) {
       
        Usertrail u=new Usertrail(-1,20);
        u.display();
       
    }
   

}






package uni;

public class IllegalValueException extends Exception {
public String toString(){
    return "Illegal Value Exception";
   
}


}

No comments:

Post a Comment