Your opinion on various TRY block constructs

While rewriting a big application, I am looking at some code construction I used before. There are no functional differences between them, because I get the desired outcome, the variable must be defined as either 0 or a value. But is one “any better” for any reason? And by extension this similar construction in ObjC or other languages too.

  1. Typical try/error:
try
	set ungroupedBoxCount to count (every page item whose item layer is couponLayer and class is not group)
on error
	set ungroupedBoxCount to 0
end try
  1. try to set the var and then go on to attempt the potentially non-returning statement
try
	set ungroupedBoxCount to 0
	set ungroupedBoxCount to count (every page item whose item layer is couponLayer and class is not group)			
end try

  1. put required value outside of try and only try the potentially non-returning statement
set ungroupedBoxCount to 0
try
	set ungroupedBoxCount to count (every page item whose item layer is couponLayer and class is not group)
end try

Hi,

only version 1 is generally reliable.
In version 2 and 3 the variable could be set to an undefined state during the execution of the line,
but probably not in this snippet.

In some rare cases where the variable could be unset but doesn’t throw an error
you have to check for the “variable is not defined” error

try
	set variable to something()
	variable
on error
	set variable to 0
end try