6
\$\begingroup\$

I know that is generally expected that you should not swallow exceptions. In this code, an Infragistic's UltraWinGrid is being configured. Is there a better way to handle the failed catches? Is this an exception to the rule?

private void HideExcludedColumns(UltraGridBase grid)
{
 if (_scExclusions == null) return;
 foreach (var strKey in _scExclusions)
 {
 //Set the appropriate column of the grid to hidden.
 foreach (var band in grid.DisplayLayout.Bands)
 {
 try
 {
 band.Columns[strKey].Hidden = true;
 break;
 }
 catch { } //go to the next band.
 }
 }
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jan 18, 2013 at 14:44
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Why do you think it should be an exception to the rule? What kind of exceptions can be thrown here, and in which cases? \$\endgroup\$ Commented Jan 18, 2013 at 14:49
  • \$\begingroup\$ I'm not sure what exceptions my be possible, other the column may not exist. But it did not write this code myself, and I'm not fully sure about how this control works. \$\endgroup\$ Commented Jan 18, 2013 at 14:59

2 Answers 2

12
\$\begingroup\$

I agree with almaz - a blanket catch is bad juju in just about every case. Need to know what exception(s) wind up being there. If the column doesn't exist, there should be a better, non-exceptional way of doing that:

 private void HideExcludedColumns(UltraGridBase grid)
 {
 if (_scExclusions == null) return;
 foreach (var strKey in _scExclusions)
 {
 //Set the appropriate column of the grid to hidden.
 foreach (var band in grid.DisplayLayout.Bands)
 {
 var columnIndex = band.Columns.IndexOf(strKey);
 if (columnIndex > -1)
 {
 band.Columns[columnIndex].Hidden = true;
 break;
 }
 }
 }
 }
answered Jan 18, 2013 at 15:32
\$\endgroup\$
5
  • \$\begingroup\$ Thanks, that looks great assuming that the only possible exception. \$\endgroup\$ Commented Jan 18, 2013 at 15:49
  • \$\begingroup\$ To expand: I am currently in the process of auditing all of the "catch-all"'s in a legacy code base at my work. It is one of the most painful things I have ever had to do. Catch-alls are badbadbad. \$\endgroup\$ Commented Jan 23, 2013 at 4:16
  • 1
    \$\begingroup\$ As a general rule, your code should only catch what it can handle and either resolve or report appropriately. \$\endgroup\$ Commented Jan 23, 2013 at 21:35
  • \$\begingroup\$ @RyanGates Do you think there are cases where a catch-rethrow is valid and not bad practice? Eg: catch { logOrDoSomething(); throw; }. I find that some state information on logged on a debug level can be infinitely useful like this. \$\endgroup\$ Commented Dec 19, 2013 at 4:32
  • 1
    \$\begingroup\$ @Kyle I think there are certain cases where it makes sense, but I would argue that they are few and far between. I have found that it holds pretty well as a general rule. \$\endgroup\$ Commented Dec 19, 2013 at 14:30
4
\$\begingroup\$

I'd introduce an assert in that catch block. That way whenever it gets entered during debug, the program will die and you can see what exception is being thrown. In production, the assert will not apply and the current behavior will remain. Then as you find exceptions being caught there, add code to handle them properly.

answered Jan 18, 2013 at 15:34
\$\endgroup\$
4
  • \$\begingroup\$ It is a good suggestion but it doesn't cover all scenarios. Murphy's law states exactly that. In production you may run into exceptions that you didn't encounter during development. It is better to have a fallback mechanism to deal with such cases. \$\endgroup\$ Commented May 29, 2014 at 9:41
  • \$\begingroup\$ @Sandeep, and what fallback mechanism would you add here? \$\endgroup\$ Commented May 29, 2014 at 15:03
  • \$\begingroup\$ Fallback mechanism might be a wrong choice of word. But after one has changed his code to handle all the possible exceptions, the rest of the code should be written as given in the previous answers, which is do not handle the error (in case of an library) or have global handlers for unhandled exception or thread exceptions. \$\endgroup\$ Commented May 30, 2014 at 6:41
  • \$\begingroup\$ @Sandeep, fair enough. I only suggested this approach as a transitional step to keep the current behavior until you're sure that you've identified all the cases it was detecting. \$\endgroup\$ Commented May 31, 2014 at 20:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.