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

Get help with using AutoHotkey and its commands and hotkeys
aircooled
Posts: 79
Joined: 01 Dec 2018, 08:51

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

22 Jan 2020, 11:13

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
	}
aircooled
Posts: 79
Joined: 01 Dec 2018, 08:51

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

22 Jan 2020, 12:37

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.
User avatar
Chunjee
Posts: 369
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

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

22 Jan 2020, 13:04

If there is I don't see it on that documentation page.
swagfag
Posts: 3302
Joined: 11 Jan 2017, 17:59

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

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.
aircooled
Posts: 79
Joined: 01 Dec 2018, 08:51

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

22 Jan 2020, 18:11

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
swagfag
Posts: 3302
Joined: 11 Jan 2017, 17:59

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

22 Jan 2020, 19:21

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
}
aircooled
Posts: 79
Joined: 01 Dec 2018, 08:51

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

23 Jan 2020, 02:43

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
	}
User avatar
TLM
Posts: 1580
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

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

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.
aircooled
Posts: 79
Joined: 01 Dec 2018, 08:51

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

24 Jan 2020, 10:19

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?
User avatar
TLM
Posts: 1580
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

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

28 Jan 2020, 13:57

aircooled wrote:
24 Jan 2020, 10:19
can the 2 statements inside the parenthesis be considered 2 different statements in 1 line?
Yes

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], hotkeyuser1234, prasad383, Rohwedder and 76 guests