Page 1 of 1

Try/Catch instead of ErrorLevel

Posted: 30 May 2018, 10:05
by swagfag
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

Re: Try/Catch instead of ErrorLevel

Posted: 30 May 2018, 13:42
by SirRFI
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'!")

Re: Try/Catch instead of ErrorLevel

Posted: 30 May 2018, 17:45
by swagfag
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?

Re: Try/Catch instead of ErrorLevel  Topic is solved

Posted: 01 Jun 2018, 01:58
by lexikos
The examples are based on the v1 examples, and are not correct for v2.

Re: Try/Catch instead of ErrorLevel

Posted: 01 Jun 2018, 03:00
by swagfag
i see. i didnt think to check v1

Re: Try/Catch instead of ErrorLevel

Posted: 06 Jun 2020, 16:26
by lexikos
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.

Re: Try/Catch instead of ErrorLevel

Posted: 06 Jun 2020, 16:33
by swagfag
im glad to see ErrorLevel gone
:thumbup:

Re: Try/Catch instead of ErrorLevel

Posted: 29 Jun 2020, 15:28
by vvhitevvizard
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.