ErrorLevel

Discuss the future of the AutoHotkey language
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

ErrorLevel

28 May 2018, 16:15

I've used ImageSearch recently, which got me confused: functions now return something, and this particular one returns 0 on not found and 1 on match, which is opposite to ErrorLevel values. However, error is still shown only in ErrorLevel as 2.

Question is: do we still need ErrorLevel in such places, if at all? I like how the function returns true/false, allowing to put it in if or other place. Perhaps errors could throw exception instead? If anything, users could probably use try/catch to prevent exception from stopping code flow if needed. This would surely save me time where I put file path first, then options, which is seemingly not allowed.
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: ErrorLevel

28 May 2018, 16:41

This is the reason why 0 should be Success... ;)
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: ErrorLevel

28 May 2018, 17:04

What is "the" reason? 0 equals false, so if (ImageSearch... wouldn't work.
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: ErrorLevel

28 May 2018, 17:47

Treat 0 as success and any other value as an error, as do many WINAPI functions. This would solve your problem.
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: ErrorLevel

28 May 2018, 18:59

I prefer 1 (true) to equal success.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: ErrorLevel

28 May 2018, 19:40

It is not about tastes or personal preferences, but about how useful and convenient it is.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: ErrorLevel

28 May 2018, 23:53

You say that there I a train why this specific behavior is useful but fail to provide it.
Recommends AHK Studio
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: ErrorLevel

29 May 2018, 00:32

nnnik wrote:You say that there I a train why this specific behavior is useful but fail to provide it.
You can do:

Code: Select all

If (ImageSearch(...))
    MsgBox "Error: " . ErrorLevel
Else
    MsgBox "OK!"
Btw: I think you made a typo in your comment. ;)
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: ErrorLevel

29 May 2018, 02:08

Yeah but you could do the same with the other way around. In fact it makes more sense the other way around:
If ( ImageSearch(...) ) ;if image search was successful
;vs.
If ( !ImageSearch(...) ) ;if image search was unsuccessful
Recommends AHK Studio
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: ErrorLevel

29 May 2018, 04:59

Some API functions return an 'error status'. They commonly return 0 (= no error) on success, otherwise one of various possible error codes.

Other API functions return a BOOL. They commonly return a non-zero value on success, otherwise 0.

If a function returns either 0 or 1, it obviously belongs to the latter kind of functions. So why should it return 0 on success?
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: ErrorLevel

29 May 2018, 05:21

just me: I don't understand your logic. :eh:
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: ErrorLevel

29 May 2018, 09:29

I can not see any reason for errorlevel existing in v2. Imagesearch return value isn't documented as far as I can see, it should probably return what errorlevel is set to now, and ofc, errorlevel shouldn't be set.

Cheers.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: ErrorLevel

29 May 2018, 10:00

Nah ErrorLeve now is used for 2 things: return values and errors.
In AHK v2 it should only return the return value part of the ErrorLevel never the error part.
Thats what throw is used for.
e.g. ImageSearch is the perfect example of a command where you shouldn't:
Helgef wrote:probably return what errorlevel is set to now
ErrorLevel is set to 0 if the image was found in the specified region, 1 if it was not found, or 2 if there was a problem that prevented the command from conducting the search (such as failure to open the image file or a badly formatted option).
Only the 0 and 1 are part of the return value. The 2 is an error type and over the long run should result in descriptive error messages.
For now just throwing "ImageSearch failed" should be fine.
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: ErrorLevel

29 May 2018, 10:02

Yes I agree nnnik :thumbup:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: ErrorLevel

29 May 2018, 10:42

- If you remove ErrorLevel you have to account for all the things here:
ErrorLevel
https://autohotkey.com/docs/misc/ErrorLevel.htm
- Take InputBox for example, InputBox's return value is the text of the Edit field. But how are you supposed to know if the InputBox was closed? Furthermore it's not an 'error' if the user closes the InputBox. I thought of A_InputBoxResult as a possible answer.
- What about DllCall and Sort?

- @Flipeador: API functions differ in whether non-zero/zero mean success/failure. AHK needs to pick one, there isn't really a decisive answer either way.
- 0 or blank on failure, otherwise a meaningful result, is one approach. 0 or blank on success, otherwise a useful error code, is another approach.
- However, true=non-zero, and false=zero is pretty much universal (unless someone else knows better). And if Func() is more intuitive if functions return non-zero to mean success. (Also, AHK functions have generally used the rule: non-zero: success, zero/blank: failure.)
Last edited by jeeswg on 29 May 2018, 10:59, edited 5 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: ErrorLevel

29 May 2018, 10:44

Return values are to be returned by the function. If they are too complex for a single variable or a single string make the return value an object.
Additionally variables that are implicitly set need to die a permanent death.
Recommends AHK Studio
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: ErrorLevel

29 May 2018, 10:58

[b]v2-changes[/b] wrote:
The translation from v1-command to function is generally as follows (but some functions have been changed, as documented further below):
  • If the command's first parameter is an output variable and the second parameter is not, it becomes the return value and is removed from the parameter list. Otherwise, RunWait returns the exit code, and any other commands which set ErrorLevel return non-zero on success and zero on failure (except for SendMessage). Most legacy functions still set ErrorLevel.
jeeswg wrote:@Flipeador: API functions differ in whether non-zero/zero are true/false.
No. They differ in whether they return an error code (where 0 stands for 'no error') or a BOOL (where True stand for success).
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: ErrorLevel

29 May 2018, 11:00

- @just me: I was just editing that bit. 'API functions differ in whether non-zero/zero are success/failure'.

- @nnnik: The Winapi functions GetLastError/SetLastError use what is effectively an implicit variable.
- The solution is either another parameter or another A_ variable (perhaps the best) or use ErrorLevel as now (or some other solution I haven't thought of, perhaps similar to or using OnError). If you return an object, it makes the InputBox function somewhat unwieldy to use, breaks scripts, it makes it inconsistent with other functions, and aesthetically the object solution would be dire. Aesthetics matter.
Last edited by jeeswg on 29 May 2018, 11:33, edited 4 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: ErrorLevel

29 May 2018, 11:10

nnnik wrote:Return values are to be returned by the function. If they are too complex for a single variable or a single string make the return value an object.
Additionally variables that are implicitly set need to die a permanent death.
Truth
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: ErrorLevel

29 May 2018, 11:32

jeeswg, do you set errorlevel in your own functions? I guess you manage just fine without it. So does everybody else and ahk will too.
kczx3 wrote:
nnnik wrote:Return values are to be returned by the function. If they are too complex for a single variable or a single string make the return value an object.
Additionally variables that are implicitly set need to die a permanent death.
Truth
Truth

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: thqby and 32 guests