Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which exception to throw when user tries to add to a full container

I am trying to write a fixed sized queue for generic type elements.The user can call the constructor, providing a size and an internal array is created for that size.(see below)

class FixedSizeQ<E>{
    private E[] q;
    private int N;

    public FixedSizeQ(int max) {
        super();
        q = (E[]) new Object[max];
        N = 0;
    }
..
}

I thought of defining an isEmpty() and isFull() methods and an insert() method for the above.If the client tries to add an element to the already full array,I want to throw an exception.Going through the javadocs,I thought IllegalStateException would be the correct exception to throw.

public boolean isFull(){
    return N == q.length;
}
public void insert(Item item){
    if(isFull()){
        throw new IllegalStateException("queue full");
    }
    ...     
}

I want to know if my understanding is correct..Someone suggested that IllegalArgumentException is more appropriate.Can someone advise?

like image 942
damon Avatar asked Aug 09 '13 07:08

damon


People also ask

What type of exceptions can you throw?

We can throw either checked or unchecked exceptions. The throws keyword allows the compiler to help you write code that handles this type of error, but it does not prevent the abnormal termination of the program.

What is exception throw?

The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.

What is the use of throw exception in C++?

Throw: When you want to generate an Exception, the Throw keyword is used to throw Exception to handle it in the run time. When you are throwing an Exception without handling it, then they need to use Throw keyword. Multiple Exceptions: You can mention various Exceptions in the throws clause.

How do you throw an exception in a message flow?

Include a THROW node in your message flow. Set the node properties to identify the source and content of the exception. By using either statement options or node properties, you can specify a message identifier and values that are inserted into the message text to give additional information and identification to users who interpret the exception.

What are the practices to avoid when throwing exceptions?

The following list identifies practices to avoid when throwing exceptions: Don't use exceptions to change the flow of a program as part of ordinary execution. Use exceptions to report and handle error conditions. Exceptions shouldn't be returned as a return value or parameter instead of being thrown.

What exceptions should not be thrown in Java?

Don't throw System.Exception, System.SystemException, System.NullReferenceException, or System.IndexOutOfRangeException intentionally from your own source code. Don't create exceptions that can be thrown in debug mode but not release mode.


1 Answers

I think you should make the insert method return boolean, and return true if the object was inserted and false if the queue is full. Adding objects to a full queue does not seem like an exceptional condition to me.

In any case, if no predefined exception in the Java API is a good match, you can create an exception type of your own to best suit the situation:

public class QueueFullException extends RuntimeException {
    // or "extends Exception" if you want it checked
    ...
}
like image 148
Joni Avatar answered Oct 09 '22 16:10

Joni