Results 1 to 2 of 2
  1. #1
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default wrongly thrown exception

    Hello. I am revising Exceptions for my exam and I encountered an error.

    I have a Car class:
    public class Car
    Java Code:
    {
        private String registration;
    
        public Car(String registration) {
            if (registration == null) throw new NullPointerException("registration");
            this.registration = registration;
        }
        
        public String getRegistration() {
            return registration;
        }
        
        public String toString() {
            return registration;
        }
    }
    Then enum type, just for clarity.

    Java Code:
    public enum CardinalPoint
    {
        WEST, EAST, NORTH, SOUTH
    }
    This class simulates traffic lights.

    Java Code:
    public class TrafficLights
    {
        private boolean color;
        private List<Car> cars = new ArrayList<Car>();
        
        public TrafficLights(boolean color) {
            this.color = color;
        }
        
        public boolean hasGreenLight() {
            return this.color;
        }
        
        public void setRed() throws TrafficLightsException {
            if(color == false) throw new TrafficLightsException();
            else color = false;
        }
        
        public List<Car> setGreen(int throughput) throws TrafficLightsException {
            if(color == true) throw new TrafficLightsException();
            if(throughput <= 0) throw new IllegalArgumentException();
    
            color = true;
            List<Car> carsGone = new ArrayList<Car>();
            for(int i = 0; i < throughput; i++) {
                carsGone.add(cars.get(i));
                cars.remove(i);
            }
            
            return cars;
        }
        
        public boolean comeTo(Car car) throws NullPointerException, IllegalArgumentException {
            boolean result = true;
            
            if(car == null) throw new NullPointerException();
            
            if(cars.contains(car) == true) throw new IllegalArgumentException();
            
            if( (color == true) && (cars.isEmpty() == true) ) result = true;
            
            if( (color == false) && (cars.isEmpty() == false) ) {
                cars.add(car);
                result = false;
            }
            
            return result;
        }
        
        public List<Car> getWaitingCars() {
            return Collections.unmodifiableList(cars);
        }   
    }
    I defined my own exception:

    Java Code:
    public class TrafficLightsException extends Exception
    {
       public TrafficLightsException() {
           super("Traffic lights exception.");
       }   
    }
    And then I tried to define class Crossroads as a simulator of crossroads.

    Java Code:
    public class Crossroads
    {
        private TrafficLights north = new TrafficLights(true);
        private TrafficLights south = new TrafficLights(true);
        private TrafficLights west  = new TrafficLights(false);
        private TrafficLights east  = new TrafficLights(false);
        private int throughput;
    
        /**
         * @param throughput Throughput of the crossroads
         * @throws IllegalArgumentException if the throughput argument is <= 0
         */
        public Crossroads(int throughput) {
            if (throughput <= 0) throw new IllegalArgumentException("throughput");
            this.throughput = throughput;
        }
        
        /**
         * Car is comming to the crossroad from given direction.
         * 
         * @param car car
         * @param dir comming direction (either north, south, west or east)
         * @return true if the car went throught the crossroads immediately,
         *         false if the car stopped
         * @throws IllegalArgumentException if the comming direction is not known
         */
        public boolean comeTo(Car car, CardinalPoint dir) {
            switch (dir) {
                case WEST:  return west.comeTo(car);
                case EAST:  return east.comeTo(car);
                case NORTH: return north.comeTo(car);
                case SOUTH: return south.comeTo(car);
                default:    throw new IllegalArgumentException("dir");
            }
        }
    
        @Override
        public String toString() {
            StringBuilder ret = new StringBuilder();
            ret = ret.append("North way has a " + (north.hasGreenLight() ? "green" : "red") + " light: ");
            ret = ret.append(north.getWaitingCars());
            ret = ret.append(System.getProperty("line.separator"));
            ret = ret.append("South way has a " + (south.hasGreenLight() ? "green" : "red") + " light: ");
            ret = ret.append(south.getWaitingCars());
            ret = ret.append(System.getProperty("line.separator"));
            ret = ret.append("West way has a " + (west.hasGreenLight() ? "green" : "red") + " light: ");
            ret = ret.append(west.getWaitingCars());
            ret = ret.append(System.getProperty("line.separator"));
            ret = ret.append("East way has a " + (east.hasGreenLight() ? "green" : "red") + " light: ");
            ret = ret.append(east.getWaitingCars());
            return ret.toString();
        }
        
        public void switchLights() {
            
            if(north.hasGreenLight() == true) {
                north.setRed();
                south.setRed();
            }
        }
    }
    The last method should reverse the lights in the crossroads, north+south and east+west always have the same light.
    My problem is in this last method, where north.setRed() call throws TrafficLightsException and it should not, because as these classes are defined, it should never throw it.
    I cant see my mistake here, so if anybody can, please let me know.
    Thanks
    Last edited by dawnMist; 11-29-2012 at 02:19 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: wrongly thrown exception

    Can we please see the stack trace?

Similar Threads

  1. Replies: 1
    Last Post: 11-03-2012, 02:42 PM
  2. Replies: 3
    Last Post: 05-02-2011, 09:02 PM
  3. Replies: 5
    Last Post: 02-15-2011, 12:39 AM
  4. After catching the exception thrown
    By scoobyrox in forum New To Java
    Replies: 2
    Last Post: 09-05-2010, 03:29 PM
  5. Which exception is thrown.....
    By money123 in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 04:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •