AHK-v2-script-converter
AHK-v2-script-converter copied to clipboard
Construction 'if ... else' with 'try' throws an "Unexpected Else" error
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
Indeed a strange error, thanks for reporting. Strange that V2 displays now an error.
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
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") }