学习java自定义异常,需要以某种方式改变实现

时间:2012-11-06 08:35:03

标签: java exception-handling

我正在学习java,我应该为一个固定队列的类添加一个异常处理程序。看来界面需要改变,但我不确定如何。

代码:

//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;
        }
}

编译器输出......

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个错误

2 个答案:

答案 0 :(得分:3)

在FixedQueue中,您有已检查的例外情况。

    public void put(char ch)
     throws QueueFullException {

    public char get()
     throws QueueEmptyException {

这意味着这些方法的界面必须具有相同的“抛出”。

BTW我会使QueueFullExceptionQueueEmptyException扩展IllegalStateException这不是检查异常,但我仍然会将它添加到界面中的throws子句中。

为了进行比较,您可以查看Queue,我会尽可能地遵循它抛出的命名和例外。

我会考虑将你的FixedBuffer变成一个环形缓冲区,也称为Circular Buffer这样你的队列就不会因为它到达终点而耗尽空间。


这就是我如何基于队列设置界面。

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();
}

通过这种方式,您可以在心理上(如果不是在代码中)将Queue<Character>替换为CharQueue正如文档说明,offer优于add,您可能想要选择其中一个取决于您的要求。

答案 1 :(得分:2)

FixedQueue课程中,您的方法put会引发QueueFullException,但您的界面ICharQ中未指定。与getQueueEmptyException相同。

你可以:

  • 在界面中也指定这些例外
  • 或者使这两个例外都延伸RuntimeException而不是Exception