tags:

views:

445

answers:

7

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?

+16  A: 

IllegalArgumentException

Chris Jester-Young
Winner by chronology. :P
Daddy Warbox
A: 

IllegalArgumen...ooh well :)

OscarRyz
+10  A: 

Winner by Accuracy: java.lang.IllegalArgumentException

Andreas Petersson
Haha nice. I already specified 'Quick Java question', though. :(
Daddy Warbox
A: 

Just incase you didnt get it..IllegalArgumentException :)

+2  A: 

Actually, I don't believe you should (being lazy or not) build your own exception for this sort of common exceptions. So there, being lazy is good... sometimes.

Ubersoldat
+8  A: 

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.

Phil
+1. In addition, if he absolutely has to have such an error prone API, then creating his own checked exception may help guide the API client to proper coding practices.
Darron
It's a private nested inner class. That's as good of an encapsulation as I can manage within Java.
Daddy Warbox
A: 

If you don't feel fear about the explosion 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 is'nt very beautiful to read: you can use if you prefer the very structured code. A simple IllegalArgumentException is more common :)

alepuzio