java - Custom Unchecked Exceptions -
okay guys i've been trying figure out past day or so. homework assignment has me creating both unchecked , checked exceptions. checked exceptions believe must handled before compiling (with try & catch or throwing next thing calls it. unchecked exceptions don't understand how custom ones work. caught @ runtime , don't need thrown or encased try & catch if they're custom how ide or whatever know for? example: 1 of custom unchecked files supposed trigger if user adds pokemon party full, how tell ide that's needs happen? exception file looks like:
public class partyisfullexception extends runtimeexception { public partyisfullexception() { super(); } public partyisfullexception(string message) { super(message); } }
and want implement in method, idk how it. realize can't throw them because user won't expecting them , therefore won't try catch them.
public void addtoparty(string name) throws partyisfullexception { boolean exists = false; (int = 0; < 152; i++) { exists = (name.equals(pokedex[i])); } if (exists) { throw new pokemonalreadyexistsexception(); } else { if (partypos < 6) { party[partypos] = name; partypos++; } else { throw new partyisfullexception(); } } }
i realize can't throw them because user won't expecting them , therefore won't try catch them.
you can throw them!
in real project, should documented.
/* * @throws partyisfullexception if ispartyfull() return true */ public void addtoparty(string name) throws partyisfullexception {...}
usually unchecked exception used situation client of method avoiding exceptional condition e.g.:
if(theparty.ispartyfull()) { // tell user party full // , can't add more pokemon } else { theparty.addtoparty(thepokemon); }
and shouldn't have explicitly catch because handling circumstance.
if exception thrown , there not try
-catch
outside, throw way terminate thread. (for small program main
, means program crashes.)
Comments
Post a Comment