Try/Catch instead of ErrorLevel Topic is solved

Discuss the future of the AutoHotkey language
swagfag
Posts: 3913
Joined: 11 Jan 2017, 17:59

Try/Catch instead of ErrorLevel

30 May 2018, 10:05

https://lexikos.github.io/v2/docs/comma ... .htm#ex_el
i tried running that, but i couldnt understand when the catch block was supposed to be triggered. When ErrorLevel != 0 ? When A_LastError != 0 ?
i stripped it down to this:

Code: Select all

try
{
	FileCopy("D:\test.txt", "D:\Backup")
}
catch
{
    MsgBox("There was a problem while backing the files up!",, 16)
    ExitApp
}

MsgBox(Format("ErrorLevel: {}`nLastError: {}", ErrorLevel, A_LastError))
the 1st time its run, it will copy text.txt(the file existing is a given) to the folder. ErrorLevel = 0, LastError = 0, catch isnt triggered as expected
the 2nd time its run, it will fail to copy the file(overwrite flag omitted, disabled by default), ErrorLevel = 1, LastError = 80 ERROR_FILE_EXISTS, yet catch still isnt being triggered

so what was supposed to happen here? it works when handling messed up COM objects, but thats because they throw runtime exceptions
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: Try/Catch instead of ErrorLevel

30 May 2018, 13:42

Apparently this function does not use throw on failure, which is why catch doesn't work. Correct me if I am wrong.

Here's a working custom example:
SirRFI wrote:

Code: Select all

myFunc2(X, Y)
{
	if !(X is "integer")
		throw Exception("Invalid argument type: expected integer, got " type(X))
	else if !(Y is "integer")
		throw Exception("Invalid argument type: expected integer, got " type(Y))
	ToolTip("It's me!", X, Y)
}

try
{
	myFunc2("b", 60.60)
	; some more code that depends on myFunc2 non-error result
	ToolTip("Uh oh, this works!") ; ...if you fix passed arguments above
}
catch error
{
	ToolTip("myFunc ended up with error, but worry not - I can handle this!`n" error.message) ; error.message, because Exception() makes an object, but as shown in docs - you can throw a simple string too!
	; some code that takes care of the problem - something you'd put in if (ErrorLevel = 2) anyway
}
MsgBox("We still rockin'!")
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.
swagfag
Posts: 3913
Joined: 11 Jan 2017, 17:59

Re: Try/Catch instead of ErrorLevel

30 May 2018, 17:45

while i do appreciate the brief excursus into exceptions, the question still stands
Apparently this function does not use throw on failure, which is why catch doesn't work.
the docs claim one can surround with try/catch as a replacement for checking errorlevel. either im grossly misunderstanding something wrt to how thats supposed to be used(syntactically), or the example given is plain wrong. so which one is it?
lexikos
Posts: 6975
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Try/Catch instead of ErrorLevel  Topic is solved

01 Jun 2018, 01:58

The examples are based on the v1 examples, and are not correct for v2.
swagfag
Posts: 3913
Joined: 11 Jan 2017, 17:59

Re: Try/Catch instead of ErrorLevel

01 Jun 2018, 03:00

i see. i didnt think to check v1
lexikos
Posts: 6975
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Try/Catch instead of ErrorLevel

06 Jun 2020, 16:26

v2.0-a110 removes ErrorLevel and therefore should make the script above work. However, it should be noted that if "D:\test.txt" does not exist, it is still not treated as an error. I think it would be more intuitive and helpful to permit 0 copied files only when a wildcard is used, so I will likely change it.
swagfag
Posts: 3913
Joined: 11 Jan 2017, 17:59

Re: Try/Catch instead of ErrorLevel

06 Jun 2020, 16:33

im glad to see ErrorLevel gone
:thumbup:
User avatar
vvhitevvizard
Posts: 404
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Try/Catch instead of ErrorLevel

29 Jun 2020, 15:28

lexikos wrote:
06 Jun 2020, 16:26
v2.0-a110 removes ErrorLevel and therefore should make the script above work. However, it should be noted that if "D:\test.txt" does not exist, it is still not treated as an error. I think it would be more intuitive and helpful to permit 0 copied files only when a wildcard is used, so I will likely change it.
Yes please. Whenever a non-wildcarded file/directory is failed to be created/moved/copied/changed-its-date/etc.

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 9 guests