I wrote this method and I want to handle UnexpectedFormatExceptions: more specifically, a MissingFieldException, an EmptyFieldException, an UnknownCardTypeException, an UnknownSpellCardException.
The problem is that I don't fully understand the idea of handling exceptions. Now, I made a class UnexpectedFormatExceptions (as mentioned earlier) and subclasses with the constructors and alike. Should I just add a try block to take the whole code and catch blocks for each exception? What would be the right course of action here?
public ArrayList<Card> loadCardsFromFile(String path) throws IOException, FileNotFoundException, UnexpectedFormatException {
String currentLine = "";
FileReader fileReader = new FileReader(path);
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(fileReader);
String[] currentsplit;
ArrayList<Card> temp = new ArrayList<Card>();
while ((currentLine = br.readLine()) != null) {
currentsplit = currentLine.split(",");
if (currentsplit[0].equals("Monster")) {
MonsterCard x = new MonsterCard(currentsplit[1], currentsplit[2], Integer.parseInt(currentsplit[5]),Integer.parseInt(currentsplit[3]), Integer.parseInt(currentsplit[4]));
temp.add(x);
}
else {
if (currentsplit[1].equals("Card Destruction")) {
CardDestruction x = new CardDestruction(currentsplit[1], currentsplit[2]);
temp.add(x);
}
if (currentsplit[1].equals("Change Of Heart")) {
ChangeOfHeart x = new ChangeOfHeart(currentsplit[1], currentsplit[2]);
temp.add(x);
}
if (currentsplit[1].equals("Dark Hole")) {
DarkHole x = new DarkHole(currentsplit[1], currentsplit[2]);
temp.add(x);
}
if (currentsplit[1].equals("Graceful Dice")) {
GracefulDice x = new GracefulDice(currentsplit[1], currentsplit[2]);
temp.add(x);
}
if (currentsplit[1].equals("Harpie's Feather Duster")) {
HarpieFeatherDuster x = new HarpieFeatherDuster(currentsplit[1], currentsplit[2]);
temp.add(x);
}
if (currentsplit[1].equals("Heavy Storm")) {
HeavyStorm x = new HeavyStorm(currentsplit[1], currentsplit[2]);
temp.add(x);
}
if (currentsplit[1].equals("Mage Power")) {
MagePower x = new MagePower(currentsplit[1], currentsplit[2]);
temp.add(x);
}
if (currentsplit[1].equals("Monster Reborn")) {
MonsterReborn x = new MonsterReborn(currentsplit[1], currentsplit[2]);
temp.add(x);
}
if (currentsplit[1].equals("Pot of Greed")) {
PotOfGreed x = new PotOfGreed(currentsplit[1], currentsplit[2]);
temp.add(x);
}
if (currentsplit[1].equals("Raigeki")) {
Raigeki x = new Raigeki(currentsplit[1], currentsplit[2]);
temp.add(x);
}
}
}
return temp;
}
try {} catch (Exception e)This is also a horrible way to try and turn yu-gi-oh into java - it'd be much better to write a general card glass, with enums as types for spell, trap, monster, and then give the cards properties such as names and descriptions. Having a separate class for each card isn't the proper way to do it, especially when yu-gi-oh has so many different cards...try {} catch (Exception e), you ignored all exceptions design.