Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the DevExpress Treelist throw a HideException regularly?

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.

From 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?

like image 605
amadib Avatar asked Jan 22 '23 03:01

amadib


2 Answers

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 the DXMouseEventArgs with an ability to set e.Handled = true. Unfortunately, the XtraTreeList doesn't currently fully support DXMouseEventArgs. 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.

like image 135
jmbucknall Avatar answered Jan 23 '23 18:01

jmbucknall


I would say it's

  1. A bug
  2. Used for message signaling the way that calling Response.Redirect will throw a ThreadAbort exception to halt all code execution.
like image 38
Chris Marisic Avatar answered Jan 23 '23 18:01

Chris Marisic