Using Error and OSError objects

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Using Error and OSError objects

Post by JnLlnd » 07 Feb 2023, 15:33

I'm just begining the transition to v2. Could someone explain how to use the OSError object?

This is the v1.1 code. The error test is normally to wait until a busy file becomes writable but you can test it here with a read-only file.

Code: Select all

#requires AutoHotkey v1.1
#NoEnv
loop
{
	FileAppend % A_Now . "`n", %A_ScriptDir%\read-only.txt
	if ErrorLevel
		Sleep 20
}
until !ErrorLevel or (A_Index > 50) ; after 1 second (20ms x 50), we have a problem
MsgBox, % (ErrorLevel ? "Error" : "OK")
ExitApp
This is my attempt to convert it to v2 so far. How can I use the OSError object in this context? The doc did not really help me in this case. So far, I've been only able to manage the error with try/catch.

Code: Select all

#requires AutoHotkey v2
err := ""
Loop
{
	Try
		FileAppend(A_Now . "`n", A_ScriptDir . "\read-only.txt")
	catch Error as err
		Sleep(20)
	else
		Break
}
until (A_Index > 50) ; after 1 second (20ms x 50), we have a problem	
if IsObject(err)
	MsgBox(err.Message)
else
	MsgBox("OK")
ExitApp
Any suggestion would be appreciated.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: Using Error and OSError objects

Post by teadrinker » 07 Feb 2023, 16:37

JnLlnd wrote:

Code: Select all

	Try
		FileAppend(A_Now . "`n", A_ScriptDir . "\read-only.txt")
	catch Error as err
Error actually is the OSError object in this case.

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Using Error and OSError objects

Post by JnLlnd » 08 Feb 2023, 18:47

Thank for the reply, teadrinker. This could be clarified in the doc. Maybe with an example. I'll post it to the Doc suggestion forum.

So the properties of the Error object are the same as any other Error object (Message, File, Line, etc.)? And my code does not need changes.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: Using Error and OSError objects

Post by teadrinker » 08 Feb 2023, 19:05

JnLlnd wrote: the properties of the Error object are the same as any other Error object (Message, File, Line, etc.)?
No, the OSError has an additional Number property which contains the error code:

Code: Select all

try FileAppend '', 'S:\nonexistent.file'
catch Error as err {
    MsgBox err.Message . '`n' . err.Number
}

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Using Error and OSError objects

Post by JnLlnd » 08 Feb 2023, 20:03

Thanks. I updated my post on the doc suggestion forum.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

Post Reply

Return to “Ask for Help (v2)”