3 Replies Latest reply: Oct 7, 2008 5:43 PM by 843785 RSS

    Correct way to handle an exception in a constructor?

    843785
      Hi I was wondering if anyone could tell me if I implemented my code correctly. It compiles and runs fine, I'm just wondering if there is a more efficient or better way to do this.

           static Cabin cabinTest;
           
      public static void main(String[] args) 
          {
                testEquals();
                testCompareTo();
                try
                {
                     cabinTest = new Cabin(5, 2, true);
                }
                catch(Exception e)
                {
                     System.out.println("Invalid Input");
                }
      public Cabin (int cabinNumber, int rooms, boolean kitchen) throws Exception
           {
                super("C" + cabinNumber, rooms==1 ? ONE_ROOM_RATE : TWO_ROOM_RATE, 
                       rooms==1? ONE_ROOM_GUESTS : TWO_ROOM_GUESTS);
                
                     this.rooms = rooms;
                     this.kitchen = kitchen;
              }