View Single Post
Old 11-08-2012, 04:22 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Looks like it'll work fine to me.
You don't need to do it at all though. Since you are throwing explicitly in the same block as the try, to me you may as well just use the condition to print the message and exit instead of throwing at all. Ideally, you would throw without a try/catch at all and let the caller decide what to do (if its a cli application it'll issue the print messages, if its a gui it will present the error). The collection shouldn't be responsible for this output at all.
You can also get around that completely if GenericFood and GenericElectronics is given a common super interface. Given that they are both allowed in the list anyway, this indicates that the T generic already matches a commonality between the two, so its questionable if it necessary for any checks in the addItem (and hence no override necessary).
Fou-Lu is offline   Reply With Quote