Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
学习java自定义异常时,需要以某种方式更改实现_Java_Exception Handling - Fatal编程技术网

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

学习java自定义异常时,需要以某种方式更改实现,java,exception-handling,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 {

我在学习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个错误

在FixedQueue中,您有已检查的异常

    public void put(char ch)
     throws QueueFullException {

    public char get()
     throws QueueEmptyException {
这意味着这些方法的接口必须具有相同的“抛出”

顺便说一句,我会使
QueueFullException
QueueEmptyException
扩展,这不是检查异常,但我仍然会将其添加到接口中的throws子句中

为了进行比较,您可以查看,我将尽可能遵循它抛出的命名和异常

我会考虑把你的固定缓冲区变成一个环形缓冲区,也就是这样,你的队列不会因为它的结束而耗尽空间。


这就是我如何根据队列设置接口的方法

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();
}
公共接口队列{
/**
*如果可以,将指定的元素插入此队列
*在不违反容量限制的情况下立即返回
*成功并抛出非法状态例外时为true
*如果当前没有可用空间。
*
*@param ch要添加的字符
*@return true(由{@link Collection#add}指定)
*如果此时无法添加元素,@将引发IllegalStateException
*由于容量限制而导致的时间
*@如果此元素的某些属性
*阻止将其添加到此队列
*/
布尔加法(char-ch)抛出非法状态异常;
/**
*如果可以,将指定的元素插入此队列
*因此,在不违反容量限制的情况下立即进行。
*当使用容量受限队列时,此方法通常是
*比{@link#add}更好,后者只能插入一个元素
*通过抛出异常。
*
*@如果元素已添加到此队列,则返回true,否则返回
*假的
*@如果此元素的某些属性
*阻止将其添加到此队列
*/
布尔提供(char-ch);
/**
*检索并删除此队列的头。如果
*队列为空。
*
*@返回此队列的头
*@如果此队列为空,则会引发NoSuchElementException
*/
char remove()抛出NoTouchElementException;
/**
*删除此集合中的所有元素。
*此方法返回后,集合将为空。
*/
无效清除();
}

通过这种方式,如果不是在代码中,您可以用
CharQueue
替换
Queue
,如文档所述,
offer
add
更可取,并且您可能希望根据您的要求选择其中一种。

在FixedQueue中,您有选中的异常

    public void put(char ch)
     throws QueueFullException {

    public char get()
     throws QueueEmptyException {
这意味着这些方法的接口必须具有相同的“抛出”

顺便说一句,我会使
QueueFullException
QueueEmptyException
扩展,这不是检查异常,但我仍然会将其添加到接口中的throws子句中

为了进行比较,您可以查看,我将尽可能遵循它抛出的命名和异常

我会考虑把你的固定缓冲区变成一个环形缓冲区,也就是这样,你的队列不会因为它的结束而耗尽空间。


这就是我如何根据队列设置接口的方法

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();
}
公共接口队列{
/**
*如果可以,将指定的元素插入此队列
*在不违反容量限制的情况下立即返回
*成功并抛出非法状态例外时为true
*如果当前没有可用空间。
*
*@param ch要添加的字符
*@return true(由{@link Collection#add}指定)
*如果此时无法添加元素,@将引发IllegalStateException
*由于容量限制而导致的时间
*@如果此元素的某些属性
*阻止将其添加到此队列
*/
布尔加法(char-ch)抛出非法状态异常;
/**
*如果可以,将指定的元素插入此队列
*因此,在不违反容量限制的情况下立即进行。
*当使用容量受限队列时,此方法通常是
*比{@link#add}更好,后者只能插入一个元素
*通过抛出异常。
*
*@如果元素已添加到此队列,则返回true,否则返回
*假的
*@如果此元素的某些属性
*阻止将其添加到此队列
*/
布尔提供(char-ch);
/**
*检索并删除此队列的头。如果
*队列为空。
*
*@返回此队列的头
*@如果此队列为空,则会引发NoSuchElementException
*/
char remove()抛出NoTouchElementException;
/**
*删除此集合中的所有元素。
*此方法返回后,集合将为空。
*/
无效清除();
}

通过这种方式,如果不是在代码中,您可以用
CharQueue
替换
Queue
,如文档所述,
offer
add
更可取,并且您可能希望根据您的要求选择其中一种。

FixedQueue
类中,您的方法
put
抛出
QueueFullException
,但在界面
ICharQ
中未指定该方法。与
get
QueueEmptyException
相同

您可以:

  • 指明