Daddy Warbox
Daddy Warbox

Reputation: 4598

Which Java exception should I use?

Let's assume I'm a complete lazy bum and I don't want to invest the several dozen keystrokes needed for my own exception class (it's not utterly important which gets used, really). However, to pretend I'm following good practices here, I want a pre-existing one that best fits my situation.

Problem: I need to throw an exception when my class's constructor receives an object in its parameters that is not found within a given list I've built elsewhere.

Which exception class would be appropriate to throw for that?

Upvotes: 4

Views: 3220

Answers (5)

alepuzio
alepuzio

Reputation: 1418

If you don't fear an explosion in the number of classes, you can extend the IllegalArgumentException for this situation.

 public class InvalidInstance extends IllegalArgumentException{
    private String[] parameter;

    public InvalidInstance (String[] param){
        this.parameter = param;
    }

    public String getMessage()
    String msg = "YOUR_MESSAGE";
    /* I think a string as "The currente object is
       invalid for parameter "+cycle for over parameter;*/
    msg += super.geTMessage();
    return msg;
}

public Constructor(parameter1,...){
    String[] param = new String[number_parameters]
    if...
    throws new InvalidInstance(param);
}

In this way, you can log all the parameters what run the exception.

This code isn't very beautiful to read: you can use if you prefer the very structured code. A simple IllegalArgumentException is more common :)

Upvotes: 0

user49300
user49300

Reputation: 58

Just in case you didn't get it, IllegalArgumentException :)

Upvotes: -2

Phil
Phil

Reputation: 815

IllegalArgumentException is indeed the answer here, but I'd say you have a problem with your design. In essence, your class invariant is dependent on the state of some external object, which is a violation of encapsulation. There's no way to determine whether a call to your constructor will succeed without knowledge of some other object, which leads to a confusing and easily misused API.

This problem is mitigated somewhat if the list you refer to is a static final unmodifiable List (see java.util.Collections.unmodifiableList()) and contained within the class in question, but I still don't like it terribly much. Better is to encapsulate, if possible, the acceptable parameter values in an enum, which will eliminate the need for an exception altogether. I generally dislike exceptions thrown from constructors. If you must throw an exception, use a factory method instead.

If an option is not available to you that eliminates the need for an external list, you may need to rethink your design.

Upvotes: 10

Andreas Petersson
Andreas Petersson

Reputation: 16518

Winner by Accuracy: java.lang.IllegalArgumentException

Upvotes: 12

C. K. Young
C. K. Young

Reputation: 223003

IllegalArgumentException

Upvotes: 20

Related Questions