John Tate
John Tate

Reputation: 772

Learning java custom exceptions, need to change implementation somehow

I'm learning java, I'm supposed to add an exception handler to a class for a fixed queue. It seems the interface needs to be changed but I'm not sure how.

Code:

//ICharQ.java
package qpack;

public interface ICharQ {
    void put(char ch);

    char get();

    void reset();
}

//QExcDemo.java
package qpack;

class QueueFullException extends Exception {
    int size;

    QueueFullException(int s) { size = s; }

    public String toString() {
        return "\nQueue is full. Max size is " + size;
    }
}

class QueueEmptyException extends Exception {
    public String toString() {
        return "\nQueue is empty.";
    }
}


//Excerpt from IQClasses.java
package qpack;

class FixedQueue implements ICharQ {
        private char q[];
        private int putloc, getloc;

        public FixedQueue(int size) {
                q = new char[size+1];
                putloc = getloc = 0;
        }

        public void put(char ch)
         throws QueueFullException {
                if (putloc == q.length-1)
                        throw new QueueFullException(q.length-1);


                putloc++;
                q[putloc] = ch;
        }

        public char get()
         throws QueueEmptyException {
                if (getloc == putloc)
                        throw new QueueEmptyException();

                getloc++;
                return q[getloc];
        }

        public void reset() {
                putloc = getloc = 0;
        }
}

Compiler output...

qpack/IQClasses.java:22: error: get() in FixedQueue cannot implement get() in ICharQ
public char get() 
            ^
   overridden method does not throw QueueEmptyException
qpack/IQClasses.java:12: error: put(char) in FixedQueue cannot implement put(char) in ICharQ
public void put(char ch) 
            ^
   overridden method does not throw QueueFullException

2 errors

Upvotes: 1

Views: 233

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533570

In FixedQueue you have the checked exceptions.

    public void put(char ch)
     throws QueueFullException {

    public char get()
     throws QueueEmptyException {

This means your interface for these method has to have the same "throws".

BTW I would make QueueFullException and QueueEmptyException extend IllegalStateException which is not a check exception, but I would still add it to the throws clause in your interface.

For comparison, you could look at the Queue and I would follow the naming and exceptions it throws as much as possible.

I would consider turning your FixedBuffer into a Ring Buffer also known as a Circular Buffer This way your queue won't run out of space just because it reaches the end.


This is how I would set out the interface by basing it on Queue.

public interface CharQueue {

    /**
     * Inserts the specified element into this queue if it is possible to do so
     * immediately without violating capacity restrictions, returning
     * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
     * if no space is currently available.
     *
     * @param ch the char to add
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     * @throws IllegalStateException if the element cannot be added at this
     *         time due to capacity restrictions
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean add(char ch) throws IllegalStateException;

    /**
     * Inserts the specified element into this queue if it is possible to do
     * so immediately without violating capacity restrictions.
     * When using a capacity-restricted queue, this method is generally
     * preferable to {@link #add}, which can fail to insert an element only
     * by throwing an exception.
     *
     * @return <tt>true</tt> if the element was added to this queue, else
     *         <tt>false</tt>
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean offer(char ch);

    /**
     * Retrieves and removes the head of this queue.  This method throws an exception if this
     * queue is empty.
     *
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    char remove() throws NoSuchElementException;

    /**
     * Removes all of the elements from this collection.
     * The collection will be empty after this method returns.
     */
    void clear();
}

This way you can mentally, if not in code, replace Queue<Character> with CharQueue As the documentation notes, offer is preferable to add and you might want to choose one of these depending on your requirements.

Upvotes: 3

Jon Newmuis
Jon Newmuis

Reputation: 26512

In your FixedQueue class, your method put throws QueueFullException, but that's not specified in your interface ICharQ. Same with get and QueueEmptyException.

You can either:

  • Specify these exceptions in the interface as well
  • OR make both of these exceptions extend RuntimeException rather than Exception

Upvotes: 2

Related Questions