Complaining on "try" statement Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
slishnevsky
Posts: 34
Joined: 07 Mar 2024, 06:50

Complaining on "try" statement

Post by slishnevsky » 07 Apr 2024, 21:21

Hi.

Why is AHK complaining on try statement?

Code: Select all

CreateTimer() {
  timeout := InputBox('Set timer interval (in minutes)', 'Timer').Value
  if (!IsInteger(timeout))
    Exit
  SetTimer(() => (
    SoundPlay('WakeUp.mp3')
    MsgBox('Сработал таймер!', 'Таймер', 'Icon! OK')
    try SoundPlay('Nonexistent.mp3') ; Complains on the usage of try statement here
  ), timeout * 60000)
}
Attachments
image.png
image.png (30.82 KiB) Viewed 183 times
Last edited by slishnevsky on 07 Apr 2024, 22:53, edited 1 time in total.

User avatar
mikeyww
Posts: 27195
Joined: 09 Sep 2014, 18:38

Re: Complaining on Try statement

Post by mikeyww » 07 Apr 2024, 21:38

  1. What does that mean?
  2. Post the script instead of an image of it.
  3. Post a screenshot of the error message.

User avatar
boiler
Posts: 17214
Joined: 21 Dec 2014, 02:44

Re: Complaining on Try statement

Post by boiler » 07 Apr 2024, 21:54

Try is not a valid part of an expression which you are trying to define as part of a fat arrow function. The syntax for that is () => expr, where expr is an expression.

slishnevsky
Posts: 34
Joined: 07 Mar 2024, 06:50

Re: Complaining on Try statement

Post by slishnevsky » 07 Apr 2024, 22:48

boiler wrote:
07 Apr 2024, 21:54
Try is not a valid part of an expression which you are trying to define as part of a fat arrow function. The syntax for that is () => expr, where expr is an expression.
Makes sense. Then, how should I stop playing audio there?
If I move try SoundPlay('Nonexistent.mp3') outside the arrow function, the interpreter doesn't complain, but the audio keeps playing...
According to the documentation, To stop a file that is currently playing, use SoundPlay on a nonexistent filename as in this example: try SoundPlay "Nonexistent.avi".

Code: Select all

CreateTimer() {
  timeout := InputBox('Set timer interval (in minutes)', 'Timer').Value
  if (!IsInteger(timeout))
    Exit
  SetTimer(() => (
    SoundPlay('WakeUp.mp3')
    MsgBox('Сработал таймер!', 'Таймер', 'Icon! OK')
  ), timeout * 60000)
  try SoundPlay('Nonexistent.mp3') ; Doesn't complain, but keeps playing
}

User avatar
boiler
Posts: 17214
Joined: 21 Dec 2014, 02:44

Re: Complaining on "try" statement  Topic is solved

Post by boiler » 07 Apr 2024, 23:46

Just use a regular function instead of a fat arrow function.

slishnevsky
Posts: 34
Joined: 07 Mar 2024, 06:50

Re: Complaining on "try" statement

Post by slishnevsky » 08 Apr 2024, 00:11

@boiler

Yes! Worked. Thank you.

Post Reply

Return to “Ask for Help (v2)”