DesktopPopupMenu issue

While raising an exception incurs additional overhead and may be considered “slower”, exceptions should be, well, exceptional. Ie., you shouldn’t get one every time through the loop. Thus as Jeremie pointed out, try/catch in and of itself shouldn’t incur additional overhead. The catch section is not intended to happen very often. Certainly, “programming by exception” can be considered bad practice.

1 Like

Then we will need to change the code from:

try
  PopupMenu1.SelectRowWithText("C")
Catch err
  //handle no string found
end try

to

var myboolean as boolean
myboolean = PopupMenu1.SelectRowWithText("C")
if myboolean = False then
  //handle no string found
end if

or something else you have in mind?

I agree with this philosophy: exceptions should be quite exceptional indeed. If a function CAN return a useful status code, especially in something as simple as a boolean, then it should.

Let us consider another common source of exceptions: I/O exceptions when writing files. There are any number of reasons why a file write might fail: the disk is full, permissions are wrong, etc etc etc. So an exception can be useful here, because there are many reasons for failure and most of them are not recoverable.

In the menu item example, though, there’s a binary result to be expected: either the menu item was found, or it wasn’t. The outcome can be expressed in a simple Boolean result.

Perhaps the function could be renamed FindAndSelectRowWithText, to suggest that the Find might fail and thus return a False result.

The exception contains a status code and descriptive text.

I dislike Xojo’s trend toward overuse of exceptions. My own subclass of PopUpMenu that I’ve used for ages returns a boolean that I can ignore if I want to by using Call, and leaves the SelectedRowIndex alone if the target isn’t found.

3 Likes

If not PopupMenu1.SelectRowWithText(“C”) then
// handle string not found
End

1 Like

Using code similar to what Christian posted? (second post)

No, mine is using the old control that doesn’t have a SelectRowWithText method. My subclass adds that and also SelectRowByTag method, as well as separate events for user and programmatic selection changes.

1 Like