OiO.lk Blog java Unreachable Catch Block for IsEmptyException and IsFullException. These exceptions are never thrown from the try statement body
java

Unreachable Catch Block for IsEmptyException and IsFullException. These exceptions are never thrown from the try statement body


In my ArrayQueueTest.java file, I can’t seem to figure out what’s going wrong. I get the same error, which is

unreachable catch block for IsEmptyException and IsFullException

I will give money to whoever helps me with this. I have tried everything from double checking imports/packages to make sure everything has the correct path. The files have a correct path. I’m at the end of the road here with this assignment, as I have no idea what is going wrong.

package apps;

import adts.ArrayQueue;
import exceptions.IsFullException;
import exceptions.IsEmptyException;

public class ArrayQueueTest {

    public static void main(String[] args) {
        ArrayQueue<String> queue = new ArrayQueue<>(3);
        try {
            queue.enqueue("A");
            queue.enqueue("B");
            queue.enqueue("C");
            System.out.println(queue);
            queue.enqueue("D");
        }
        catch (IsFullException e) {
            System.out.println(e.getMessage());
        }
        catch (IsEmptyException e) {
            System.out.println(e.getMessage());
        }
        try {
            System.out.println("Dequeue: " + queue.dequeue());
            System.out.println(queue);
            queue.dequeue();
            queue.dequeue();
            queue.dequeue();
        }
        catch (IsFullException e) {
            System.out.println(e.getMessage());
        }
        catch (IsEmptyException e) {
            System.out.println(e.getMessage());
        }
    }
}

package adts;

import interfaces.QueueInterface;
import exceptions.IsFullException;
import exceptions.IsEmptyException;

public class ArrayQueue<E> implements QueueInterface<E> {

    protected E[] queue;  
    protected int front = 0;
    protected int rear = 0;
    protected int size = 0;

    protected final int DEFAULT_CAPACITY = 5;
    @SuppressWarnings("unchecked")
    public ArrayQueue() {
        queue = (E[]) new Object[DEFAULT_CAPACITY];
    }

    @SuppressWarnings("unchecked")
    public ArrayQueue(int capacity) {
        queue = (E[]) new Object[capacity];
    }


    @Override
public void enqueue(E element) throws IsFullException {
    if (isFull()) {
        System.out.println("Queue is full, throwing IsFullException"); // 
        throw new IsFullException("Out, out, brief candle! The queue is ful");          
    }
    queue[rear] = element;
    rear = (rear + 1) % queue.length;
    size++;
}

@Override
public E dequeue() throws IsEmptyException {
    if (isEmpty()) {
        System.out.println("Queue is empty, throwing IsEmptyException"); //   
        throw new IsEmptyException("The queue is empty! All we have is     time!");
    }
    E temp = queue[front];
    queue[front] = null;
    front = (front + 1) % queue.length;
    size--;
    return temp;
 }


    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean isFull() {
        return size == queue.length;
    }

    @Override
    public String toString() {
        StringBuilder qStr = new StringBuilder("\nQueue: ");
        for (int i = 0; i < size; i++) {
            qStr.append(queue[(front + i) % queue.length]).append(" ");
        }
        return qStr.toString();
    }
}

package exceptions;

public class IsEmptyException extends Exception
{
    public IsEmptyException(String message) 
    {
        super(message);
    }
}
package exceptions;

public class IsFullException  extends Exception
{
    public IsFullException(String message) 
    {
        super(message);
    }
}
package interfaces;

import exceptions.IsEmptyException;
import exceptions.IsFullException;

public interface QueueInterface<E> {

    void enqueue(E element) throws IsFullException;  // add an element to      the queue - always at the end of the queue

    E dequeue() throws IsEmptyException;   // remove and return the front of the queue

    boolean isEmpty();

    boolean isFull();

}



You need to sign in to view this answers

Exit mobile version