Question

public class ArrayQueue{
    private Object[] theArray;
    private int currentSize;
    private int front;
    private int rear;
    static final int DEFAULT_CAPACITY=10;
    public  ArrayQueue(){
        theArray=new Object[DEFAULT_CAPACITY];
        makeEmpty();    
    }
    public void makeEmpty(){
        currentSize=0;
        rear=-1;
        front=0;
    }

    public void enqueue(Object x) throws OverFlow{
        if (isFull())
            throw new OverFlow("Array size exceeded");
        else{
            rear=increment(rear);
            theArray[rear]=x;
            currentSize++;
            }
        }

    public Object dequeue()throws UnderFlow{
        if (isEmpty())
            throw new UnderFlow("Empty array");
        else{
            Object returnValue=theArray[front];
            theArray[front]=null;//check if this has to be done
            front=increment(front);
            currentSize--;
            return returnValue;
        }
    }

    public Object getFront() throws UnderFlow{
        if (isEmpty())
            throw new UnderFlow("Empty array");
        else
            return theArray[front];

    }

    public boolean isEmpty(){
        if (currentSize==0)
            return true;
        else
            return false;
    }

    public boolean isFull(){
        if (currentSize==theArray.length)
            return true;
        else
            return false;
    }

    public int increment(int x){
        if (x+1==currentSize)
            x=0;
        else
            x++;
        return x; 
        }

public static void main (String args[]){
    ArrayQueue q=new ArrayQueue();
    q.enqueue("1");
}


}



public class OverFlow extends Exception{
    public OverFlow(){
        super();
    }
    public OverFlow(String s){
        super(s);

    }
}


public class UnderFlow extends Exception{
    public UnderFlow(){
        super();
    }
    public UnderFlow(String s){
        super(s);

    }
}  

When I try to run this I get an error as unreported exception OverFlow,Must be caught or declared to be thrown.
I am new to Java and programming but I have to learn a data structures course.Therefore if someone can tell me whats wrong here and how to correct it it would be really helpful

Était-ce utile?

La solution

Any class that extends Exception (with the exception of RuntimeException) is considered a checked exception. This means that you, the programmer, must either catch it in a try...catch block, or throw the exception elsewhere.

The problem is that your method enqueue() throws a checked exception.

You could solve this one of two ways:

  • Wrap the call to enqueue in a try...catch block, or
  • Add throws OverFlow to main.

Examples of both:

try {
    q.enqueue("1");
} catch (OverFlow e) {
    e.printStackTrace();
}


public static void main(String[] args) throws OverFlow {
    ArrayQueue q=new ArrayQueue();
    q.enqueue("1");
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top