Throw an error about a wrong parameter of a function - with the name of this parameter Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Throw an error about a wrong parameter of a function - with the name of this parameter

04 Oct 2020, 16:12

If you run the following Hotstring() example,

Code: Select all

Hotstring(":*:btw", "by the way", "WrongArg")
there will be thrown an error:
Error: Parameter #3 invalid.
Specifically: WrongArg
How to achieve the same effect (that is, to report the name of the wrong argument) with a custom function?
I know there is A_ThisFunc variable, but I don't know about anything like A_ThisFuncArgs.

Code: Select all

func(parameter) {
    if parameter not in foo,bar,baz
        throw
    else
        return "success"
}

func("aaa")  ; Should throw an error
Last edited by john_c on 04 Oct 2020, 16:27, edited 1 time in total.
User avatar
boiler
Posts: 16963
Joined: 21 Dec 2014, 02:44

Re: Throw an error about the wrong parameter of the function - with the name of this parameter  Topic is solved

04 Oct 2020, 16:27

Code: Select all

func(parameter) {
	if parameter not in foo,bar,baz
		throw, "Parameter is invalid.`nSpecifically: " parameter
	return "success"
}

func("aaa")
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Throw an error about a wrong parameter of a function - with the name of this parameter

05 Oct 2020, 07:57

john_c wrote:
04 Oct 2020, 16:12
(...) How to achieve the same effect (...)
I know your question is more about finding a way to report the name of the wrong argument but I should also mention that you can use the exception function to mimic the way an error is thrown when you call the Hotstring built-in function passing it a wrong argument (in particular, incriminate the line that actually called the function, and not a line in the arcanes of the function):

Code: Select all

func(parameter) {
    if parameter not in foo,bar,baz
        throw Exception("Parameter #1 invalid.", -1, parameter)
    else return "success"
}
; try func("aaa")
; catch exception {
	; MsgBox, 64,, % exception.extra
; }
func("aaa")

A_AhkUser
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ArkuS, Ineedhelplz and 229 guests