Page 1 of 1

Writing a try/catch statement in ONE (1) line

Posted: 22 Jan 2020, 11:13
by aircooled
Is it possible to write this statement in a single line? Don't ask me why, I just want to do it.

I am thinking of semi colon separated, or colon, or comma or something else.

Everything in a single line:

Code: Select all

    loop
    {
	try (X1.Range("A1").Value := "=ROW()", error:=0)
	catch error
	sleep 10
	if (!error)
		break
	}

Re: Writing a try/catch statement in ONE (1) line

Posted: 22 Jan 2020, 11:17
by Chunjee
https://www.autohotkey.com/docs/commands/Try.htm

I think closest you could do is a function that does a try/catch inside.

Re: Writing a try/catch statement in ONE (1) line

Posted: 22 Jan 2020, 12:37
by aircooled
Chunjee wrote:
22 Jan 2020, 11:17
https://www.autohotkey.com/docs/commands/Try.htm

I think closest you could do is a function that does a try/catch inside.
OK, yeah, that could be an option. But the question I wish to anwer now is can the above or any group of commands be written in one line, semicolon separated or whatever?

This can be done to a certain degree in other programming languages.

Re: Writing a try/catch statement in ONE (1) line

Posted: 22 Jan 2020, 13:04
by Chunjee
If there is I don't see it on that documentation page.

Re: Writing a try/catch statement in ONE (1) line

Posted: 22 Jan 2020, 13:18
by swagfag
u can chain functions with a comma a(), b(), c()(not recommended)
u can chain some commands, legacy IfEqual(super not recommended)
u cant chain keywords

also i dont understand ur construct, ure misusing the concept of try/catch:

Code: Select all

    loop
    {
	try (X1.Range("A1").Value := "=ROW()", error:=0)
	catch error
	sleep 10
	if (!error)
		break
	}
the break should go inside the try-block. u shouldnt have to check catch's argument outside the catch-block.

Re: Writing a try/catch statement in ONE (1) line

Posted: 22 Jan 2020, 18:11
by aircooled
swagfag wrote:
22 Jan 2020, 13:18
u can chain functions with a comma a(), b(), c()(not recommended)
u can chain some commands, legacy IfEqual(super not recommended)
u cant chain keywords

also i dont understand ur construct, ure misusing the concept of try/catch:

Code: Select all

    loop
    {
	try (X1.Range("A1").Value := "=ROW()", error:=0)
	catch error
	sleep 10
	if (!error)
		break
	}
the break should go inside the try-block. u shouldnt have to check catch's argument outside the catch-block.
OK, so to have several commands colon separated like in VBA

for i = 1 to 10: ProcessFoo(i): next

Can't be done in AHK. Sad.

Not at all. The try/catch is correct and is doing its job just fine. Thanks

Re: Writing a try/catch statement in ONE (1) line

Posted: 22 Jan 2020, 19:21
by swagfag
at all, at all. something can work correctly and be misused still despite that

Code: Select all

loop
{
	try
	{
		X1.Range("A1").Value := "=ROW()"
		break
	}
	catch
		sleep 10
}

Re: Writing a try/catch statement in ONE (1) line

Posted: 23 Jan 2020, 02:43
by aircooled
Can anyone On Topic tip on how to write a series of commands in one (1) line like it can be done in other programming languages.

It's important (don't know if it makes a difference) that it includes a try/catch of a COM Interface call, for example:

Code: Select all

 loop
    {
	try (X1.Range("A1").Value := "=ROW()", error:=0)
	catch error
	sleep 10
	if (!error)
		break
	}

Re: Writing a try/catch statement in ONE (1) line

Posted: 23 Jan 2020, 09:01
by TLM
aircooled wrote:
22 Jan 2020, 11:13
Is it possible to write this statement in a single line?
The short answer is no.

Re: Writing a try/catch statement in ONE (1) line

Posted: 24 Jan 2020, 10:19
by aircooled
TLM wrote:
23 Jan 2020, 09:01
aircooled wrote:
22 Jan 2020, 11:13
Is it possible to write this statement in a single line?
The short answer is no.
Ok, thanks.

One last question. Observing at the try block above:

Code: Select all

try (X1.Range("A1").Value := "=ROW()", error:=0)
can the 2 statements inside the parenthesis be considered 2 different statements in 1 line?

Re: Writing a try/catch statement in ONE (1) line

Posted: 28 Jan 2020, 13:57
by TLM
aircooled wrote:
24 Jan 2020, 10:19
can the 2 statements inside the parenthesis be considered 2 different statements in 1 line?
Yes