When PowerShell is fed a line (or block) of code - it evaluates that block (or line) as a whole
In this case, your first line is: If ($error -ne $null)
PowerShell knows how to interperate the line, but its invalid syntax because PowerShell doesn't have a "what to do" block in the event that the criteria is met.
If you feed PowerShell the block as a whole - it recognises the "what to do block" and knows where it needs to go next:
Are you able to elabourate more on which systems and platforms you are finding that accept the single lines? I have just tested this on Win10 (PowerShell 5), 2012R2 (Powershell 4 and 5), Windows 7 (PowerShell 4 and 5) and they all behave exactly the same.