GooglePrettify

2019年7月16日 星期二

Dos Batch Files - Error Handling

I generally find the conditional command concatenation operators much more convenient than ERRORLEVEL.
yourCommand && (
  echo yourCommand was successful
) || (
  echo yourCommand failed
)
There is one complication you should be aware of. The error branch will fire if the last command in the success branch raises an error.
yourCommand && (
  someCommandThatMayFail
) || (
  echo This will fire if yourCommand or someCommandThatMayFail raises an error
)
The fix is to insert a harmless command that is guaranteed to succeed at the end of the success branch. I like to use (call ), which does nothing except set the ERRORLEVEL to 0. There is a corollary (call) that does nothing except set the ERRORLEVEL to 1.
yourCommand && (
  someCommandThatMayFail
  (call )
) || (
  echo This can only fire if yourCommand raises an error
)

from : https://stackoverflow.com/questions/1164049/batch-files-error-handling

沒有留言:

張貼留言