首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >学习java自定义异常,需要以某种方式更改实现

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

Stack Overflow用户
提问于 2012-11-06 16:35:03
回答 2查看 206关注 0票数 1

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

代码:

代码语言:javascript
复制
//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;
        }
}

编译器输出...

代码语言:javascript
复制
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个错误

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-06 16:38:08

在FixedQueue中,您有检查过的异常。

代码语言:javascript
复制
    public void put(char ch)
     throws QueueFullException {

    public char get()
     throws QueueEmptyException {

这意味着这些方法的接口必须有相同的“抛出”。

顺便说一句,我会让QueueFullExceptionQueueEmptyException扩展IllegalStateException,这不是一个检查异常,但我仍然会将它添加到您接口的throws子句中。

为了进行比较,您可以查看Queue,我将尽可能多地跟踪它抛出的命名和异常。

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

这就是我如何通过基于队列来设置接口。

代码语言:javascript
复制
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,正如文档所指出的那样,offeradd更可取,并且您可能希望根据自己的需求选择其中之一。

票数 3
EN

Stack Overflow用户

发布于 2012-11-06 16:38:09

FixedQueue类中,方法put抛出QueueFullException,但接口ICharQ中没有指定这一点。getQueueEmptyException也是如此。

您可以执行以下任一操作:

  • 也可以在接口中指定这些异常,
  • 或者使这两个异常都扩展RuntimeException而不是Exception
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13247231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档