Command class throws incorrect exception

Topics: CAB & Smart Client Software Factory
Apr 1, 2006 at 9:09 AM
originally posted by: Tom_Whitner

Lines 184-185 of Command.cs result in a FormatException being thrown rather than the expected CommandException.

Currently the lines read:

throw new CommandException(String.Format(CultureInfo.CurrentCulture,
Resources.CannotGetCommandAdapter, invoker.GetType()));

In the resource file:

Resources.CannotGetCommandAdapter = 'The Command {0} cannot get an adapter for the type "{1}"'

These lines are intended to throw an exception when the appropriate command adapter cannot be located. However, the template (Resources.CannotGetCommandAdapter) is defined with 2 replacement parameters, and only 1 is provided (invoker.GetType()). This causes a FormatException to be thrown instead of the original CommandException. Pretty nasty.

The corrected line would provide the command name as a parameter also.

throw new CommandException(String.Format(CultureInfo.CurrentCulture,
Resources.CannotGetCommandAdapter, this.Name, invoker.GetType()));

Admittedly, this won't happen often, but we did encounter it, and it was a little difficult to figure out why we were getting the FormatException rather than the CommandException.

Thanks,
Tom