Edit: I'm dealing only with tools installed with the OS from Windows XP, so no "choice" or anything, just basic DOS batch.
Are there any caveats to using logical OR || after a command for generic error handling?
I haven't seen it, but are there any commands that issue different errorlevels based on the type of failure they encounter? If not, why use errorlevels at all when you can use logical OR? Edit: Err, though errorlevels are obviously useful where logical OR can't be used (for loops, etc) or when doing more complex error handling, but my question still remains...
Perhaps most importantly, what about handling special characters in path/file names, strings/variables, or user input where an escape character can't be used, or even causes problems?
I think that most problems would arise when requiring user input and they use special characters like < > | " (and to a lesser extent, ! and ^) which make cmd crash anywhere later. The only workaround I know is to try and predict these failures which make the script/cmd crash by separating the susceptible code and loading it in an internal child instance of cmd, and then error handling that instance as it crashes back to the main script. (Of course, if you need to pass a variable, it has to be done through redirection to a file or the registry.)
Here's my example workaround for the handling in this case:
Main script:
Code: Select all
:retry
cmd /c user_input.bat 2>nul || (echo Failed. No special characters allowed.
goto retry)
user_input.bat would just be anything that has "set /p" and does something to the input - where the failure occurs encountering a special character.
So, am I at the peak of batch error handling or is there more?