try/end try not catching all errors?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
stam
Posts: 3069
Joined: Sun Jun 04, 2006 9:39 pm

try/end try not catching all errors?

Post by stam » Thu Nov 18, 2021 11:15 am

hi all,

i have a simple setup to give feedback to users for a variety of actions - by showing a transient status line of text. The text is a background group that has handlers to show a message and hide the message.

Rather than writing 'dispatch' code everywhere i wanted to create a stack-wide handler that would delegate the dispatching to the group to simplify calling the handler.

So, lets say the group has the code:

Code: Select all

on showMessage pMessage
    set the text of field "text" of me to pMessage
    set the visible of me to true
end showMessage

on hideMessage
    set the text of me to "Message" // so this can be seen in the IDE when showing invisibles
    set the visible of me to false
end showMessage
Rather then having to write everywhere:
dispatch "showMessage" to group "messageGroup" of this card with "my amazing message", I implement a stack handler:

Code: Select all

on showMessage pMessage
    try
        dispatch "showMessage" to group "messageGroup" of this card with pMessage
    catch e
        put "messageGroup is not placed on this card"
    end try
end showMessage

on hideMessage
    try
        dispatch "hideMessage" to group "messageGroup" of this card
    end try
end hideMessage
so then in my code i can just write showMessage "my amazing message" for example- much easier to remember when coming back to this months later ;)

The issue is that not all cards have the background group "messageGroup" - sometimes this is intentional, sometimes accidental.
If the group is not on the card it calling showMessage pMessage does not trigger any error and nothing is put into the messageBox...

So my question is why doesn't the catch statement fire? (just to alert me i may have forgotten to place the group).
Or is this some peculiarity with groups/background groups? Or am I misusing the try/catch block?

Stam

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1232
Joined: Thu Apr 11, 2013 11:27 am

Re: try/end try not catching all errors?

Post by LCMark » Thu Nov 18, 2021 11:54 am

@stam: Dispatch doesn’t throw an error if a message is not handled/found - instead it returns what happened in ‘it’ (iirc).

stam
Posts: 3069
Joined: Sun Jun 04, 2006 9:39 pm

Re: try/end try not catching all errors?

Post by stam » Thu Nov 18, 2021 12:47 pm

LCMark wrote:
Thu Nov 18, 2021 11:54 am
@stam: Dispatch doesn’t throw an error if a message is not handled/found - instead it returns what happened in ‘it’ (iirc).
Thanks Mark,
actually i don't think that's quite it (or at least not the source of the conundrum).

I created a button with just the showMessage call to the stack handler in it's mouseUp handler and placed a breakpoint.

It seems code execution is immediately diverted to the background group, whether it's on the card or not, which his probably why no error shows up anywhere (also - 'it' remains empty)

Is this intended? seems weird to me...

If this is intended behaviour, what's the best way to determine if a background group is on a card?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: try/end try not catching all errors?

Post by bn » Thu Nov 18, 2021 2:25 pm

Hi Stam,

I tried to replicate your set-up in very simple stack that I attach below.

Card 1 has the background group "messageGroup" removed, that group is on card 2.

From the message box I issue:

Code: Select all

showMessage "my amazing message" && the long time
And it works as intended, on card 1 I get in the message box "messageGroup is not placed on this card", on card 2 the message is placed in the field of the group.

Kind regards
Bernd
Attachments
messagePath.livecode.zip
(1.21 KiB) Downloaded 161 times

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1232
Joined: Thu Apr 11, 2013 11:27 am

Re: try/end try not catching all errors?

Post by LCMark » Thu Nov 18, 2021 2:48 pm

@stam: Heh - sorry - I misread what you were doing... Send will throw if a message is not handled by anything / dispatch won't but that's not what would cause an error here is the resolution of the object itself...

Bernd seems to have a simple example which does do as you would expect so I'm not sure why your code is not working in your app.

However, I can propose an alternative to trying to catch an error - check for the existence of the object first:

Code: Select all

if there is a group "..." then
   ... send message to group ...
end if
Doing that means errors thrown by the message you are sending won't get swallowed up.

stam
Posts: 3069
Joined: Sun Jun 04, 2006 9:39 pm

Re: try/end try not catching all errors?

Post by stam » Thu Nov 18, 2021 6:42 pm

LCMark wrote:
Thu Nov 18, 2021 2:48 pm
However, I can propose an alternative to trying to catch an error - check for the existence of the object first:

Code: Select all

if there is a group "..." then
   ... send message to group ...
end if
Thanks Mark and Bernd - will certainly look at Bernd's example to try and figure out why the result is different.

@mark - i tried exactly that initially, which also didn't work (ie it would always report there is a group, even when the background group is not on the card) - which is why i then tried the try/catch block.
Maybe there's just something funky with my stack :-/

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: try/end try not catching all errors?

Post by mwieder » Mon Nov 22, 2021 5:43 pm

Stam- how about:

Code: Select all

on showMessage pMessage
         dispatch "showMessage" to group "messageGroup" of this card with pMessage
         if it is "unhandled" then
        	put "messageGroup is not placed on this card"
        end if
end showMessage

on hideMessage
         dispatch "hideMessage" to group "messageGroup" of this card
         if it is "unhandled" then
        	put "messageGroup is not placed on this card"
        end if
end hideMessage

Post Reply