1

Okay guys I've been trying to figure this out for the past day or so. My homework assignment has me creating both Unchecked and Checked Exceptions. The checked exceptions I believe I get basically they must be handled before compiling (With try & catch or throwing it to the next thing that calls it. For unchecked exceptions I don't understand how custom ones work. They are caught at runtime and don't necessarily need to be thrown or encased with try & catch but if they're custom how does the IDE or whatever know what to look for? Example: One of my custom Unchecked files is supposed to trigger if the user adds a pokemon but the party is full, but how do I tell the IDE that that's what needs to happen? My Exception file looks like:

public class PartyIsFullException extends RuntimeException {

    public PartyIsFullException() {
        super();
    }

    public PartyIsFullException(String message) {
        super(message);
    }
}

and then I want to implement it in this method, but idk how to do it. I realize now I can't throw them because the user won't be expecting them and therefore won't try to catch them.

public void addToParty(String name) throws PartyIsFullException {
        boolean exists = false;
        for (int i = 0; i < 152; i++) {
            exists = (name.equals(pokedex[i]));
        }
        if (exists) {
            throw new PokemonAlreadyExistsException();
        } else {
            if (partyPos < 6) {
            party[partyPos] = name;
            partyPos++;
            } else {
                throw new PartyIsFullException();
            }
        }
    }
Joseph hooper
  • 967
  • 4
  • 16
  • 35
  • You don't tell the IDE, and the IDE *doesn't* know. – user253751 Mar 21 '15 at 00:36
  • "the user won't be expecting them" - why not? Your documentation should mention the exception. – user2357112 Mar 21 '15 at 00:36
  • @immibis What I mean by that is I was given a driver (that I cannot edit) to run once I created my methods and exceptions and it does not use the try and catch block in the methods that these unchecked exceptions occur, so I figure there is some way to trigger these exceptions. Do I just use an if else and throw the unchecked message with a message like I did in my code above? Or am I going about this wrong? – Joseph hooper Mar 21 '15 at 00:45
  • To make an exception be thrown, you throw an exception. Thee is nothing more to it. – user253751 Mar 21 '15 at 00:46
  • You could use if/else condition to throw exception. It is ok to do that. – minion Mar 21 '15 at 00:49
  • @immibis okay so maybe I was misunderstanding the concept of throwing an exception. I can throw an unchecked exception and not expect anyone using it to ever catch it in a block somewhere in their code? – Joseph hooper Mar 21 '15 at 00:49
  • @immibis Thanks for clearing that up! – Joseph hooper Mar 21 '15 at 00:54

2 Answers2

2

I realize now I can't throw them because the user won't be expecting them and therefore won't try to catch them.

You can throw them!

In a real project, it should be clearly documented.

/*
 * @throws PartyIsFullException if isPartyFull() would return true
 */
public void addToParty(String name) throws PartyIsFullException {...}

Usually an unchecked exception is used for a situation where the client of the method is avoiding the exceptional condition themselves e.g.:

if(theParty.isPartyFull()) {
    // tell the user the party is full
    // and they can't add more Pokemon
} else {
    theParty.addToParty(thePokemon);
}

And thus they shouldn't have to explicitly catch it because they are already handling that circumstance.

If the exception is thrown and there is not a try-catch outside, it will throw all the way up to terminate the thread. (For a small program with just main, this means the program crashes.)

Radiodef
  • 37,180
  • 14
  • 90
  • 125
0

Although you don't know if a certain custom unchecked exception could or not be thrown from a certain method you still can catch it (or it's superclass Exception). Having custom exception against provided by Java could add some useful details specific for your exceptional case (i.e. exception name should be meaningful so that it makes it easier to read your logs).

Custom (unchecked) exception creation and it's content could also be tuned to suit your particular need. E.g. PartyIsFullException constructor could take a Party object and format it's state into a String.

In case you don't need to facilitate your unchecked exception creation/presentation it's OK to go with RuntimeException assuming you provide a descriptive message.

S. Pauk
  • 5,208
  • 4
  • 31
  • 41
  • I was given a driver (that I cannot edit) to run once I created my methods and exceptions and it does not use the try and catch block in the methods that these unchecked exceptions occur, so I figure there is some way to trigger these exceptions. Do I just use an if else and throw the unchecked message with a message like I did in my code above? Or am I going about this wrong? – Joseph hooper Mar 21 '15 at 00:45
  • @Josephhooper The way you throw it in your code example is valid. – S. Pauk Mar 21 '15 at 00:50