nus-cs2113-AY2122S2 / forum

3 stars 2 forks source link

Applying exceptions in super and subclasses #32

Open quitejasper opened 2 years ago

quitejasper commented 2 years ago

For the iP, I wanted to try and implement an exception where if the description was missing, I would send a message where "description for a (todo/event/deadline) is missing". However, I don't want to implement try-catch blocks within a switch statement which I use to check for commands.

I read online that it's also tricky to implement unique exceptions for classes in an inheritance hierarchy (super and subclasses, in this case Task vs Todo/Deadline/Exception). For now I've created a generic exception that would tell the user that the description is missing, but how should I go about being able to override the message for subclasses Todo/Deadline/Event?

okkhoy commented 2 years ago

I will let the class discuss this :-)

wli-linda commented 2 years ago

I don't think these are best practices, but one way I got around this was to refactor my code such that user command parsing and the creation of new Todo/Deadline/Event objects are handled by separate methods. This allows the switch statement to follow SLAP (single-level of abstraction principle).

I'm also not too sure how to handle different kinds of exceptions, though. I made my custom exceptions take in a different error message when each Todo/Deadline/Event method throws them, so that any exceptions caught from the DukeException class will just print the error message supplied (in accordance with the subclass object handled by method), but I don't know how good I feel about this :'(

okkhoy commented 2 years ago

the creation of new Todo/Deadline/Event objects are handled by separate methods

Separate methods or separate classes?

WRT custom exceptions, see if this post helps/gives you an idea of what you can think about.