Try Catch Loop Crushes after a certain amount of iterations


#1

Can anyone explain why is this function crushing max after 30 iterations of Catch?

(
	attempt = 1

	fn function_A =
	(
		try
		(
			for y=1 to 8 do 
			(
                           /*generate error*/
				error()
			)

		)
		catch
		(
			print(getCurrentException())
			format "Try again\n"
			sleep 1
			format "Attemp % \n" attempt
			attempt +=1
			function_A()
		)
	)


	for z=1 to 3 do
	(
		function_A()
	)
	
)

#2

it recursively makes try/catch nesting … and luckily for you it has a limit

just agree, that is better when a program crashes than you after endless waiting :wink:


#3

Thanks, DenisT! I suspected it was a build in limit based on the exact number of iterations before the crush. Can this limit be overwritten ?


#4

not for MXS


#5

I’m using this approach to check and wait until a file is available for writing (unlocked).What would be the best alternative in this context ?


#6

first of all you have to define what ‘error()’ is


#7

Let’s assume this scenario where the error is – Runtime error: FileStream cannot create sFile

fn function_A sFile=
(
	try
	(
		local stream = openFile fFile mode:"r+"

		for y=1 to 8 do 
		(
			format "Try %\n" y
			sleep 1
		)

		close stream
	)
	catch
	(
		print(getCurrentException())
		format "Try again\n"
		sleep 1
		format "Attemp % \n" attempt
		attempt +=1
		function_A()
	)
)

#8

the second is to not use recursion…

(
	my_limit = 100
	attempts = 1
	while attempts <= my_limit do
	(
		try 
		(
			-- do something
		)
		catch (attempts += 1)
	)
	if attempts > my_limit do -- warning !!!
)

#9

it must be a reason for that. i’m pretty sure it has to be solution via .net for example to check file free or existence


#10

Excellent ! Not using recursion works better job for this task. Thank you!

Any memory constraints that i should be aware when using high numbers of attempts ?


#11

time and reason


#12

again… i would try to check why i can’t open a file for reading/writing first.

the first what is coming to my mind is to use FileSystemWatcher


#13

just for your reference… a recursion in MXS is pretty expensive for time and memory. Try to avoid it if possible and not necessary


#14

Thanks DenisT!

I found some good posts about FileSystemWatcher from your previous answers on this forum. I’ll have a look and start exploring the .net solution too.


#15

i’m pretty sure i did :wink: