Error handling - ahk-v2

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Albireo
Posts: 1778
Joined: 16 Oct 2013, 13:53

Error handling - ahk-v2

Post by Albireo » 22 Apr 2024, 17:23

Have questions regarding how error handling of instructions can/should be handled.

If I use the instruction PixelGetColor.
What different problems can arise?
The Error information for this instruction is ("An OSError is thrown on failure.")
When the link, which leads to the Error information, is opened OSError information I get more confused.

What can go wrong with eg. PixelSearch?
Can the error message be tested? (How?)
User avatar
RaptorX
Posts: 395
Joined: 06 Dec 2014, 14:27
Contact:

Re: Error handling - ahk-v2

Post by RaptorX » 22 Apr 2024, 19:00

It is very rare that the operating system throws errors with certain functions but I could envision PixelGetColor throwing an error if you pass invalid parameters or the operating system cannot access the "Screen information" so to speak. Keep in mind that I am oversimplifying here.

Most of the time the only thing you have to do is catch the error and look at A_LastError to know what the actual issue might be, like so:

Code: Select all

Try color := PixelGetColor(100, 100)
Catch
    MsgBox A_LastError 
You might want to log it to a file or depending on the error try to solve it.

an example of a NON OS Error might be this:

Code: Select all

Try color := PixelGetColor("a string here", 100)
Catch Error as err
    MsgBox err.what "`n" err.message
Notice that in this situation the error was not raised by the operating system so you will not get an OS_Error but instead a different type of error.

If you want to know a little bit more about error handling and what the different types of error objects you could get then suggest reading the Error help file.
Projects:
AHK-ToolKit
Post Reply

Return to “Ask for Help (v2)”