Page 1 of 1

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

Posted: 04 Oct 2020, 16:12
by john_c
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

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

Posted: 04 Oct 2020, 16:27
by boiler

Code: Select all

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

func("aaa")

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

Posted: 04 Oct 2020, 16:32
by john_c
Duh. Thanks.

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

Posted: 05 Oct 2020, 07:57
by A_AhkUser
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

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

Posted: 06 Oct 2020, 15:55
by john_c
@A_AhkUser Thanks a lot, useful :-)