Closed Leeyp closed 3 years ago
I don't see a problem in the way you are doing (catching many different exceptions). In fact, that seems the right way, where you are handling individual exceptions separately, differently.
When you make all under DukeException
it becomes a broad-based catching.
Even if you make all the custom exceptions sub-classes of DukeException
, it is best to catch them individually (child-first, parent-next).
Great, thank you prof. I will leave it like this then. I was just concerned if it was too long or ugly.
try { Parser.executeCommand(Parser.input, command); } catch (InvalidCommandException e) { Ui.showInvalidCommandError(); } catch (EmptyInputException e) { Ui.showEmptyInputError(); } catch (StringIndexOutOfBoundsException e) { Ui.showNoTimeAddedError(); } catch (InvalidEventTimeException e) { Ui.showInvalidEventTimeError(); } catch (InvalidDeadlineTimeException e) { Ui.showInvalidDeadlineTimeError(); } catch (NumberFormatException e) { Ui.showInvalidIntegerTaskIndexError(); } catch (IndexOutOfBoundsException e) { Ui.showTaskIndexNotExistsError(); }
The above is my code. As you can see, there are many exceptions I tried to catch. But, I didn't know you can shrink them all down to this:
public void run() { ui.showWelcome(); boolean isExit = false; while (!isExit) { try { String fullCommand = ui.readCommand(); ui.showLine(); // show the divider line ("___") Command c = Parser.parse(fullCommand); c.execute(tasks, ui, storage); isExit = c.isExit(); } catch (DukeException e) { ui.showError(e.getMessage()); } finally { ui.showLine(); } } } Source: Week 7, A-MoreOOP. How to do that?