I have been working with the DevExpress Filter TreeList code and am curious about why it throws a DevExpress.Utils.HideException
.
My understanding is that exceptions are expensive and should be used sparingly and only in certain situations, but the code snippit below shows that we are always throwing the HideException
without any specific events or code being tripped.
FilterTreeList.cs
private void OnMouseDown(object sender, MouseEventArgs e)
{
if ( e.Button != MouseButtons.Left )
return;
TreeListHitInfo hitInfo = ((TreeList)sender).CalcHitInfo(e.Location);
if ( hitInfo.HitInfoType == HitInfoType.Column )
{
ColumnInfo colInfo = ((TreeList)sender).ViewInfo.ColumnsInfo[hitInfo.Column];
GridFilterButtonInfoArgs filterButtonInfo = GetFilterButtonInfoArgs(colInfo);
if ( filterButtonInfo != null && filterButtonInfo.Bounds.Contains(e.Location) )
{
filterButtonInfo.State = ObjectState.Pressed;
((TreeList)sender).InvalidateColumnHeader(hitInfo.Column);
throw new HideException();
}
}
Why are they throwing a HideException
here, and what benefit does it serve?
It's a program flow mechanism for clearing up the control's environment. Although it is true that exceptions are expensive compared to normal code (the archetypal example is using FormatExceptions in a loop that converts strings to ints -- hence the need for TryParse type methods), when compared to major changes in the UI, fetching data from the database, etc, they are very cheap and easy to maintain.
The demo code you're quoting is that exact scenario: the control is about to refresh its entire contents. The end-user has clicked on a specific icon to perform a specific action -- the exception is not "always" being thrown at all. My only beef with this sample code (and I stress that it is sample code) is that the action is being done at mouse down and not at mouse up.
I guess the argument is "could exceptions be used for this kind of macro program flow, or should we institute a hard-and-fast rule that they should only be used for error reporting?" But that's a whole other question.
Update
I'm told by the WinForms team:
HideException
is our internal exception that is used to prevent default mouse events processing. We agree it's an old-fashioned way to stop code execution especially given that we already have theDXMouseEventArgs
with an ability to sete.Handled = true
. Unfortunately, the XtraTreeList doesn't currently fully supportDXMouseEventArgs
. We'll be adding this functionality in the next minor version and update the E2474 example accordingly.
So it'll be of archaeological interest in a month's time or so.
I would say it's
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With