AHK-v2-script-converter icon indicating copy to clipboard operation
AHK-v2-script-converter copied to clipboard

Construction 'if ... else' with 'try' throws an "Unexpected Else" error

Open xIrbis opened this issue 2 years ago • 3 comments

V1 Process, Exist, notepad.exe if !ErrorLevel try Run notepad.exe else MsgBox Process Notepad.exe already exists! Return

V2 ErrorLevel := ProcessExist("notepad.exe") if !ErrorLevel try Run("notepad.exe") else MsgBox("Process Notepad.exe already exists!") Return

The variant without "TRY" (just Run(..) ) works correctly, but it is necessary in my working script. If you manually add parentheses, this also works.

V2 ErrorLevel := ProcessExist("notepad.exe") if !ErrorLevel { try Run("notepad.exe") } else MsgBox("Process Notepad.exe already exists!") Return

xIrbis avatar Feb 05 '23 08:02 xIrbis

Indeed a strange error, thanks for reporting. Strange that V2 displays now an error.

dmtr99 avatar Mar 24 '23 19:03 dmtr99

Its because the else is paired to the try, not paired to the if

see here: https://www.autohotkey.com/boards/viewtopic.php?f=37&t=114516

for some reason, v2 allows 'try' to have an 'else'. here's how the parser sees the code:

ErrorLevel := ProcessExist("notepad.exe")
if !ErrorLevel
   try
      Run("notepad.exe")
   else
      MsgBox("try didnt throw exception")
Return

solution, use braces

mmikeww avatar Mar 25 '23 16:03 mmikeww

It also seems that you could shorten the code to remove the ErrorLevel variable, though the converter script doesn't seem to do such refactoring.

You should be able to use: if ProcessExist("notepad.exe") try Run("notepad.exe")

or if ProcessExist("notepad.exe") { try Run("notepad.exe") }

slycordinator avatar Jun 26 '23 04:06 slycordinator