问题
When doing
ChooseMenuItem("Analysis", "Particles", "Analyze Particles")
I sometime get "Invalid index" error window. Is there a way to catch that error? Doing this,
try {
ChooseMenuItem( "Analysis", "Particles", "Analyze Particles" )
}
catch {
okdialog("error")
}
does not catch the error. The "Invalid index" error is likely an error after the menu action "Analyze Particles" is done. Can anyone point out on how to catch this error? And finding out the origin of this error is a great plus. I am using GMS 1.84.
回答1:
I think the problem you're encountering is that the Particle-Analysis is running (at least partly) on a separate background-thread.
I don't believe there is a way to directly catch these exceptions in this case.
I don't use GMS 1.84 anymore, but I did try things on GMS 3.2 which you might also want to do to understand better what's going on.
First, your Try/Catch loop is OK, but if you don't put a 'break' in the catch, then the exception will nevertheless be elevated to the system, once the catch-section is left, i.e. you often want to do:
Try{
... }
Catch{
...
break
}
...
To test how scripting behaves on exceptions from a called method, I first wrote a little script and 'installed' it as menu command, once with and once without background-threading. I installed them via the File-menu in the Custom menu with command names BT and nBT, respectively:
// $BACKGROUND$
Result( "\nStart and wait" )
number i = 0
while( i < 100 ){
i++
sleep(0.05)
if ( ShiftDown() ) break
if ( OptionDown() ) Throw("Broken")
Result( "." )
}
Result("\nDone and exit.")
and
Result( "\nStart and wait" )
number i = 0
while( i < 100 ){
i++
sleep(0.05)
if ( ShiftDown() ) break
if ( OptionDown() ) Throw("Broken")
Result( "." )
}
Result("\nDone and exit.")
Then I used the 'ChooseMenuItem()' to do the testing in the following script:
string name = TwoButtonDialog("Background threaded?", "yes", "no" ) ? "BT" : "nBT"
number success = 0
Try{
Result( "\n Calling: " + name )
success = ChooseMenuItem("Custom","",name)
}
catch
{
Result("\n Caught exception." )
break
}
result("\n Success: " + success )
Testing with this combination (and using the ALT key to throw an exception in the routine) I could verify that the commands behave as should be expected:
- If the routine started by the
ChooseMenuItem
command is launched on the main-thread, then the execution of that call 'blocks' the main script until it is completed - either at its end, or when it throws and exception. The main script correctly catches exceptions and prints the result.- If the routine started by the
ChooseMenuItem
command is launched on a separate (background) thread, then the main-script continues immediately.ChooseMenuItem
returns successfully at once (if it could launch the command), and the Try/Catch loop is exited. Any exception thrown by the called routine on the background thread will not be caught anymore.
As for the origin of the error: The "Invalid index" message points to some object being removed (or kept in scope) by the main-script which is expected to be there (or no longer there) by the called background routine. This could be an image or imageDocument or the display of an image or any object (ROI, mask...) on an imageDisplay.
I suspect your main script is doing things like closing images once used? If the "analysis" is on a separate thread, your main script might be too fast or too slow and get things out of sync. You might need to add artifical pauses (sleep()
) and a more sophisticated system of keeping track of images ( using the image-IDs ) in the main script to avoid such things.
Using ChooseMenuItem()
is a workaround hack solution, so any bug-preventing solution for your problem is likely also a code-hack with some ugly 'creativeness' needed.
回答2:
Found the partial answer.
I have two commends proceeding ChooseMenuItem("Analysis","Particles","Analyze Particles")
,
ChooseMenuItem("Analysis", "Particles", "Close")
ChooseMenuItem("Analysis", "Particles", "Find Particles")
Error happens at the 2nd commend. But it is caused by the first commend. It should be a bug with GMS 1.84, where the "closing" action throw particle index out of sync. The error is gone when commenting out the fist commend ("Closing" action).
来源:https://stackoverflow.com/questions/48123649/dm-script-catch-error-with-analyze-particles