Archive: How many IfErrors do I really need?


How many IfErrors do I really need?
  I have a sequence of registry entry writes like this:

WriteRegStr HKLM "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\MyProduct" "MyProduct v 1.0" "MyCompany's Product"

>WriteRegStr HKLM "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\MyProduct" "UninstallString" "$INSTDIR\${UNINSTALLER_NAME}"
>WriteRegStr HKLM "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\MyProduct" "DisplayName" "MyProduct v1.0"
>WriteRegDWORD HKLM "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\MyProduct" "NoModify" 0x1
WriteRegDWORD HKLM"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\MyProduct" "NoRepair" 0x1
>
I need to be able to log everything that goes wrong. So, my question is: should I have a ClearErrors call before each one and an IfErrors call after each one? Is this the only way to handle errors? What is the best way to deal with so many goto labels?

Any advice is appreciated.

Thanks.

The error flag resets only if ClearErrors or IfErrors is called. You should add ClearErrors before the first WriteRegStr just to be sure. You can add IfErrors after each WriteReg if you really want to, but you can also add just one after all of the WriteRegs. :)


ClearErrors

WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyProduct" "MyProduct v 1.0" "MyCompany's Product"
>WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyProduct" "UninstallString" "$INSTDIR\${UNINSTALLER_NAME}"
>WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyProduct" "DisplayName" "MyProduct v1.0"
>WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyProduct" "NoModify" 0x1
WriteRegDWORD HKLM"Software\Microsoft\Windows\CurrentVersion\Uninstall\MyProduct" "NoRepair" 0x1
IfErrors 0+2
DetailPrint "Writing Uninstall registry keys failed!"

Anytime you call IfErrors, the system clears the error flag. It's probably good to clear errors at the beginning, but not necessary afterward if you are using IfErrors.

If you need to log specifics for each registry write, then you'll need to call IfErrors each time. But if you want to just log one error for the entire block, then call IfErrors once at the end.

And if you need to call after each time, the easiest way would be to wrap the call into its own macro. Something like this (using logiclib):


!define WriteReg "${WriteReg}"

!macro WriteReg type hive key value
${Switch} ${type}
Case 'str'
WriteRegStr '${hive}' '${key}' '${value}'
${Break}
Case 'dword'
WriteRegDword '${hive}' '${key}' '${value}'
${Break}
${EndSwitch}
${If} ${Errors}
; put code here if errors...
${Else}
; put any non-error code here, if any
${EndIf}
!macroend


Then, your calls would look like this:

${WriteReg} "str" HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyProduct" "MyProduct v 1.0" "MyCompany's Product"
${WriteReg} "str" HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyProduct" "UninstallString" "$INSTDIR\${UNINSTALLER_NAME}"
${WriteReg} "str" HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyProduct" "DisplayName" "MyProduct v1.0"
${WriteReg} "dword" HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyProduct" "NoModify" 0x1
${WriteReg} "dword" HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyProduct" "NoRepair" 0x1