This comes up every now and then, IMHO exit and exit /b are broken since they only set the errorlevel used by batch files, but they do not set the exit code of the cmd.exe process.
If a batch script is doing the errorlevel checking, call is enough:
REM DoSomeAction.cmd
@echo off
call someprogram.exe
if errorlevel 1 exit /b
REM MainScript.cmd
@echo off
...
call DoSomeAction.cmd
if errorlevel 1 (
...
)
But if you want to use && or || syntax (myscript.cmd&&someotherapp.exe
) or your script is started from a program and not another batch file, you actually want to set the process exit code (Retrieved with GetExitCodeProcess in the parent process)
@echo off
call thiswillfail.exe 2>nul
if errorlevel 1 goto diewitherror
...
REM This code HAS to be at the end of the batch file
REM The next command just makes sure errorlevel is 0
verify>nul
:diewitherror
@%COMSPEC% /C exit %errorlevel% >nul
Using a "normal" exit /b
and then calling it with call myscript.cmd&&someotherapp.exe
does work, but you can't assume that every program that executes a batch file will create the process as cmd.exe /c call yourscript.cmd