0

This is a general topic to discuss on ERRORLEVEL returned by executables in Windows. I am writing a script to install Apache HTTP server as a windows service.

httpd -k install 

The httpd executable returns

  • ERRORLEVEL 1 when there is an error in config file.
  • ERRORLEVEL 2 when there is already a service installed.
  • ERRORLEVEL 0 for success.

Is it ok to handle errors based on the ERRORLEVEL returned by the executables in general? Do we need to relly on other means to handle return codes of commands that we run in our batch scripts

vivekIndia
  • 128
  • 2
  • 11
  • Related posts: [What are the ERRORLEVEL values set by internal cmd.exe commands?](http://stackoverflow.com/q/34987885) and [Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?](http://stackoverflow.com/q/34968009) – aschipfl Aug 26 '16 at 09:10

1 Answers1

1

every programmer is free to handle exitcodes (returned by %errorlevel%) to his own gust. There is a general agreement, that 0 means "success", while any non-zero value means "an error of some sort". But you can't rely on that.

A good example is ping. If you ping a non-existent IP within your own network, it returns
Answer from <localhost>: destination unreachable.
You might think, that's unsuccessful and therefore expect an non-zero errorlevel, but obvioulsy the programmer thought "well, there is an answer, so it's successful"

Some internal commands don't touch the errorlevel, some will always return zero, some will always return 1 (robocopy, thanks aschipfl).
Executables do set the errorlevel (thanks Aacini), but how depends on the programmer. Some always zero, most of them actually usable errorlevels.

So sorry to have to tell you: you can't rely on general rules for %errorlevel%. You have to check it out for each and every single command/executable (either by documentation (if you are very lucky) or by trying)

Stephan
  • 47,723
  • 10
  • 50
  • 81
  • ...just an opposite example: `robocopy` returns a non-zero `ErrorLevel` even in case of success... – aschipfl Aug 26 '16 at 09:06
  • Just a small note: _all_ executables contained in `*.exe` files modify the errorlevel, just an internal command may preserve the errorlevel. – Aacini Aug 26 '16 at 13:42