debugcsharpModerate
Handling failed catches
Viewed 0 times
handlingcatchesfailed
Problem
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.
}
}
}Solution
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;
}
}
}
}Code Snippets
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;
}
}
}
}Context
StackExchange Code Review Q#20680, answer score: 12
Revisions (0)
No revisions yet.