How to handle the error: call to non-existent function
I'm looking for a way, catching the error: "call to non-existent function" via errorlevel or something like this.
So when I start the script and ahk comes to the FunctionThatDoesNotExist() I want to avoid stopping the program. Instead it should be possible to react by the program to this error.
Any help would be appreciated!
Best regards,
Andi
v1.0.47 released: Stdlib, RegisterCallback, NumGet/Put posted by Chris
Added dynamic function calling. [developed by lexiKos]
Description: A function (even a built-in function) may be called dynamically via percent signs. For example, %Var%(x, "fox") would call the function whose name is contained in Var. Similarly, Func%A_Index%() would call Func1() or Func2(), etc., depending on the current value of A_Index. The called function's definition must exist explicitly in the script, either via #Include or a non-dynamic call to a library containing the function. If the function does not exist -- or if the wrong number or type of parameters is passed to it -- the expression containing the call produces an empty string.
Func=SubStr Result := %Func%( "Skan",1,2 ) If ( Result = "" ) { MsgBox, 16, Fatal Error,Function %Func%() Not Defined,5 ExitApp } Else MsgBox, % Result Func=SubString Result := %Func%( "Skan",1,2 ) If ( Result = "" ) { MsgBox, 16, Fatal Error,Function %Func%() Not Defined,5 ExitApp } Else MsgBox, % Result
When I understand it right, the hint from BoBo with #ErrorStdOut stops the script. But I'm searching more for a solution that Skan descibes that keeps the script running. But additional I want to add the missing function to the lib folder.
So I tried the following, but there is one thing I don't understand: When the last but one line is missing, it leads to an error :roll:
Func=SubStr Result := %Func%( "Skan",1,2 ) If ( Result = "" ) { MsgBox, 16, Fatal Error,Function %Func%() Not Defined,5 ExitApp } Else MsgBox, % Result ifNotExist %A_Scriptdir%\lib\subString.ahk { ifNotExist %A_Scriptdir%\lib FileCreateDir, %A_Scriptdir%\lib FileAppend, ( ;only for testing Substring(String, StartingPos, Length) { return SubStr(String, StartingPos, Length) } ), %A_Scriptdir%\lib\SubString.ahk } Func=SubString Result := %Func%( "Skan",1,2 ) If ( Result = "" ) { MsgBox, 16, Fatal Error,Function %Func%() Not Defined,5 ExitApp } Else MsgBox, % Result ;The function above: ;Func=SubString ;Result := %Func%( "Skan",1,2 ) ;works only, when the following line exists!? Result := Substring( "Skan",1,2 ) MsgBox, % Result
I have to correct me: The first time you run the script, the last but one line stops the script. So when you run the first time without it, you see the error message from the msgbox. But when you run it a second time with the last but one line it doesn't work?!
AHK reads the whole script before it will execute a single line of it.
You will need two scripts, where the first script will check for dependencies and set things right and then load the target script.
I skiped the following sentence :oops:
The called function's definition must exist explicitly in the script, either via #Include or a non-dynamic call to a library containing the function.