Tuesday 18 October 2016

Exception In java Id name and price


 Id is Not Nagative  And Name Is Only A to Z And Price is 0 to 9   Value Allow Exception



import java.io.*;
import java.util.*;
class NonNegId extends Exception
{
          String msg;
          NonNegId(String m)
          {
                   this.msg = m;
          
          public String toString()
          {
                   return this.msg;
          }
}
class Bname extends Exception
{
          String bnm;
          Bname(String nm)
          {
                   this.bnm = nm;
          
          public String toString()
          {
                   return this.bnm;
          }
         
         
         
}
class PrOnly extends Exception
{
          String str;
          PrOnly(String s)
          {
                   this.str = s;
          
          public String toString()
          {
                   return this.str;
          }
}


class Book
{
          int bid;
          String bname;
          double bprice;
          Book(int no,String nm,double pr) throws NonNegId,PrOnly,Bname
          {
                  

                   if(no<0)
                   {
                             throw new NonNegId("Book Id Can not be Nagative.");
                   }
                   for(int i=0;i< nm.length();i++)
                   {
                             if(nm.charAt(i)>='0' && nm.charAt(i)<='9')
                             {
                                                          throw new Bname("Not a perfect String");
                             }
                   }
                  
                  
                  
                   if(pr < 0){

            
            throw new PrOnly("Only supported for Price 0 to 10");

        }else{
                            
                             this.bid=no;
                             this.bname=nm;
                             this.bprice=pr;
                  
        }

          }
         
         
          public static void main(String[] args) throws NonNegId,PrOnly
          {
                  
                     try{

                   Book b= new Book(1,"ja0va",120.5);
            
        }catch(NonNegId eid){

            System.out.println(eid);         
            

        }catch(PrOnly eprice){

            System.out.println(eprice);         

        }catch(Bname enm){

            System.out.println(enm);         

        }finally{

            System.out.println("Releasing resources");         

        }

    }
                  
}



No comments:
Write comments

Labels

ADVERTISEMENT