ON ERRORLEVEL not being trapped

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
Jun 8, 2010
4
0
#1
Having a problem with ON ERRORLEVEL, I can't get it to work if the errorlevel is set when an external subroutine is called.
I created 3 files to demonstrate 1) Main.btm 2) GenerateErrorLevel.btm 3) Sub.btm, Running Main.btm shows how the errorlevel is not generated at the sub level and gets generated within the main batch

TCC 11.00.49 Windows XP [Version 5.1.2600]
TCC Build 49 Windows XP Build 2600 Service Pack 3


:********************************
:Main.btm File Listing
ON ERRORLEVEL 1 GOTO ErrHandler
ON ERRORMSG GOTO ErrHandler
GOSUB "sub.btm" SomeLabel
echo Passes External GoSub
GOSUB SomeLabel
ECHO Normal Termination
quit
:SomeLabel
Call GenerateErrorLevel 1
return
:ErrHandler
Echo Error generated
CANCEL 1
:********************************
:Sub.btm File Listing
:SomeLabel
Call GenerateErrorLevel 1
return

:********************************
:GenerateErrorLevel.btm File Listing
quit %1
:********************************
:Main.btm output
Passes External GoSub
Error generated
 

rconn

Administrator
Staff member
May 14, 2008
10,292
90
#2
> Having a problem with ON ERRORLEVEL, I can't get it to work if the
> errorlevel is set when an external subroutine is called.
That's not going to work. For reasons of compatibility, ERRORLEVEL is not
assigned (or checked) when you return from a CALL (either an explicit one or
an implicit one as with your external GOSUB). Changing that would result in
breaking a *lot* of batch files written for CMD.EXE.

You could kludge around this with something like:

if %? == 1 seterror 1

after your GOSUB call.

Rex Conn
JP Software