Page 1 of 1

Clarifying Error and OSError object

Posted: 08 Feb 2023, 18:55
by JnLlnd
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)

Re: Clarifying Error and OSError object

Posted: 08 Feb 2023, 19:54
by JnLlnd
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)