How To know where the error has occurred ?

Propose new features and changes
nigelle
Posts: 10
Joined: 23 Oct 2013, 09:58
Location: France

How To know where the error has occurred ?

13 Dec 2014, 08:12

To know where the error has occurred helps debugging the script...
The structure of my scripts is :

#Include %A_ScriptDir%\lib1.ahk ; lib1 contains functions and variables that are used by most of my scripts
#Include %A_ScriptDir%\lib2i.ahk ; lib2i contains functions and variables that are used by this (i) group of my scripts
; many lines of code
...
exit
; functions that are particular to this script

When an error occurs I have the habit to show a message with msgbox and write in a log file before exit (unrecoverable error) or continue (recoverable error).

AHK basic
Most of my script have been written at the time of AHK basic and there was no many tools to know where the error has occurred...
So I maintain a variable called "wher" (without e to avoid problem with a would-be "where" key word) describing what this functional part (10 to 30 lines) of the script does.
Each function uses A_LineNumber as parameter and put it with its name (A_ThisFunc) and wher (+ if needed, other informations e.g. a_index of the loop) in the error message. So I can find where the error has occurred. The draw-back is that a function funct() that should need no parameters is coded funct(A_LineNumber) in the main script, funct(A_LineNumber+1000) in lib1 and funct(A_LineNumber+2000) in lib2i (the script, lib1 and lib2i have all less than 1000 lines)...

AutoHotkey_L
Of course I can use the same method... It still work with the Ansi version of AHK_L.
Can I simplify the coding by using Try, Catch, Throw, Finally and the call stack ?
I wish to obtain something like :
"At line n1 of the main script scriptname the function funct1 was called, it called funct2 of lib1 at line n2, that called funct3 of lib2i at line n3, ..., error %exception object%"
or more concisely :
"scriptname n1 funct1
lib1 n2 funct2
lib2i n3 funct3
...
error
What: The name of the command or function which was executing or about to execute when the error occurred.
File: The full path of the script file which contains the line at which the error occurred.
Line: The line number at which the error occurred.
Message: An error message or ErrorLevel value.
Extra: Additional information about the error, if available."
Please clarify what contains (in the call stack or error object and in case of #Include in the main script) :
- "File:" the main script or the library name ?
- "Line:" the line number in the library or in a main script with the library included in it (very not user friendly !)
From experience, A_LineNumber used in the library gives the line number in the library.
nigelle
Posts: 10
Joined: 23 Oct 2013, 09:58
Location: France

Re: How To know where the error has occurred ?

15 Dec 2014, 09:33

After more thinking :
In "what:" it would be useful to have the list of parameters passed to the function or the script.
User avatar
joedf
Posts: 8986
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: How To know where the error has occurred ?

15 Dec 2014, 10:10

To set variables during execution, you need a debugger.
There's DGp or SciTE4AHK.
Id say that the for SciTE4AHK is easier to use since it was specifically designed for autohotkey. (both use the "autohotkey.exe /debug" of course )
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
vasili111
Posts: 747
Joined: 21 Jan 2014, 02:04
Location: Georgia

Re: How To know where the error has occurred ?

15 Dec 2014, 10:20

Also to list all variables you can use that code:

Code: Select all

ListVars
Pause
DRAKON-AutoHotkey: Visual programming for AutoHotkey.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 12 guests