-1

I have the following lines of code, and I'm supposed to write an IllegalArgumentException exception, such that when I run it ,it returns "Poland already exists.". I've been reading on exceptions a lot but I just can't figure out how I am supposed to write the IllegalArgumentException, what to put in its constructor, etc (I just know I must have a constructor and a getMessage() method for the message.)

More specifically: how do I change the "cause" for the IllegalArgumentException: how do I make it check for the similarity in name of the countries?

  public static void main(String[] args) {
    Country country1 = new Country("Poland");

    try{
        Country country = new Country("Poland");
    }
    catch(IllegalArgumentException ie){
        System.out.println(ie.getMessage());
    }
}
Juggl3r
  • 3
  • 1
  • 5
  • Making an IllegalArgumentException isn't going to be much more than [choosing which constructor(s) to use](http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.html). The answer to your question probably actually depends on your Country class. For example, what does it mean for Poland to already exist? How do you keep track of that (if you do)? – Radiodef Jan 25 '14 at 14:32
  • I don't, that's why I don't understand how am I supposed to check for the similarity in names. Considering that the above is my whole main method, it should be somewhere in the Country class or in the exception class, but I don't understand exceptions well enough to say, that's why I'm confused ? – Juggl3r Jan 25 '14 at 14:41
  • Basically it seems like you're supposed to modify Country. The answer @MarkoTopolnik wrote shows you almost the exact logic you'd need to use for the exception besides keeping track of instances of Country. – Radiodef Jan 25 '14 at 14:56
  • but that would mean having no getMessage method, and as I said, my main method has to be the exact same as the above, therefore I must create a new custom exception for it...and that brings me back to square 1 – Juggl3r Jan 25 '14 at 15:03

3 Answers3

0

You don't need to define a new exception class, you just need to throw the existing one.

public Country(String name) {
  if (INSTANCES.contains(this)) 
    throw new IllegalArgumentException(name + " already exists.");
}

A bit more code is needed to maintain the INSTANCES set.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • the problem is that I'm supposed to use the exact format of the above main method, and so I must have a .getMessage method, and therefore, must have a new exception class... – Juggl3r Jan 25 '14 at 15:00
  • IllegalArgumentException already has that method. It returns what you passed to the constructor. – Marko Topolnik Jan 25 '14 at 16:56
0

Something as simple as

class IAE
{
    public static void main(String args[])
    {
        throw new IllegalArgumentException("Poland doesn't exist");
    }
}

Will output

Exception in thread "main" java.lang.IllegalArgumentException: Poland doesn't exist
    at IAE.main(iae.java:6)

Alternatively you could subclass the exception, but from what you describe it doesn't seem necessary.

Community
  • 1
  • 1
jedwards
  • 29,432
  • 3
  • 65
  • 92
  • the whole of my main method must be the above, I am supposed to use a try-catch block, I don't see how your answer helps me in any way... – Juggl3r Jan 25 '14 at 14:42
0

With the exception: it's really just a matter of constructing a new one. All exceptions are already set up with the getMessage method. So you just instantiate one with the message you want.

See IllegalArgumentException(String).

The duplicate Country thing is a whole other matter. This kind of depends on the Country class itself but a really simple way is just to use a Set<String>:

class Country {
     private static final HashSet<String> EXISTING = new HashSet<String>();

     private final String name;

     Country(String name) {
         if(EXISTING.contains(name))
             throw new IllegalArgumentException(name + " already exists!");

         this.name = name;
         EXISTING.add(name);
     }
}

That's OK but if Country is more complicated maybe it isn't that simple. There's one other problem which is that this means that countries can only be constructed once only, even if they are garbage collected.

If Countries can be "reused" per-se, it's possible to keep track of them weakly like the following:

class Country {
    private static final HashMap<String, Reference<Country>> EXISTING = (
        new HashMap<String, Reference<Country>>()
    );

    private final String name;

    Country(String name) {
        WeakReference<Country> existing = EXISTING.get(name);
        if(existing != null && existing.get() != null)
            throw new IllegalArgumentException(name + " already exists!");

        this.name = name;
        EXISTING.put(name, new WeakReference<Country>(this));
    }
}

That might be overkill though. Either of those would work with your main method. Here it is working in Ideone: http://ideone.com/uhDCzM

Radiodef
  • 37,180
  • 14
  • 90
  • 125