Clarifying Error and OSError object

Share your ideas as to how the documentation can be improved.
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Clarifying Error and OSError object

Post by JnLlnd » 08 Feb 2023, 18:55

In this post, I explained that the documentation about OSError was unclear to me (at least with my background). Maybe this could be explained with an example.
The following subclasses of Error are predefined:

MemoryError: A memory allocation failed.
OSError: An internal function call to a Win32 function failed....
Here is an example using a read-only file to test the error message.

Code: Select all

Loop ; wait until a file can be written, or abort after 1 second
{
	Try
		FileAppend(A_Now . "`n", A_ScriptDir . "\read-only.txt")
	catch Error as err ; err contains the predefined OSError object
		Sleep(20)
	else
		Break
}
until (A_Index > 50)
if IsObject(err)
	MsgBox(err.Message)
: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

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

Re: Clarifying Error and OSError object

Post by JnLlnd » 08 Feb 2023, 19:54

Update with the additional field .Number of the OSError object:

Code: Select all

Loop ; wait until a file can be written, or abort after 1 second
{
	Try
		FileAppend(A_Now . "`n", A_ScriptDir . "\read-only.txt")
	catch Error as err ; err contains the predefined OSError object
		Sleep(20)
	else
		Break
}
until (A_Index > 50)
if IsObject(err)
	MsgBox(err.Message . " " . err.Number)
: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 “Suggestions on Documentation Improvements”