[Solved] preventing the call return

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
allal
Posts: 34
Joined: 04 Jun 2011 05:49

[Solved] preventing the call return

#1 Post by allal » 16 Jul 2011 12:23

is there a way to prevent returning to calling command without using exit

like

Code: Select all

call script.cmd 
rem we don't want to return here
echo mission failed&goto :eof

::------------------------------------------------

rem we are in script.cmd
goto label

:label
if not exist file.txt (
  rem throw a terminating error to prevent returning or use goto end
  throw no such file &rem throw is not supported
  goto end
 
)

:end
rem must end here without using exit
rem please cmd forget the call



im calling a script so using the goto command will not work
Last edited by allal on 17 Jul 2011 04:48, edited 2 times in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: preventing the call return

#2 Post by aGerman » 16 Jul 2011 12:43

This code returns to the calling command, but exit /b 1 returns ErrorLevel 1, thats why goto :eof is executed.

Code: Select all

@echo off &setlocal
call :label||goto :eof
echo file exists
pause
goto :eof

:label
if not exist file.txt (
  echo no such file &pause>nul
  exit /b 1
)
goto :eof

Regards
aGerman

PS:
Because you edited your post after that ...
It s the same thing. Return an ErrorLevel from script.cmd using exit /b.

allal
Posts: 34
Joined: 04 Jun 2011 05:49

Re: preventing the call return

#3 Post by allal » 16 Jul 2011 13:02

aGerman wrote:This code returns to the calling command, but exit /b 1 returns ErrorLevel 1, thats why goto :eof is executed.

Code: Select all

@echo off &setlocal
call :label||goto :eof
echo file exists
pause
goto :eof

:label
if not exist file.txt (
  echo no such file &pause>nul
  exit /b 1
)
goto :eof

Regards
aGerman


it is something like

Code: Select all

@echo off &setlocal
call :label
if errorlevel 1 echo we returned mission failed&goto :eof
echo file exists
pause
goto :eof

it returned to call statement as this will return it to the calling batch as well
please is there a way like the throw command in other language that will force
a terminating error instantly to prevent any further processing without exiting

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: preventing the call return

#4 Post by aGerman » 16 Jul 2011 13:13

Hmm, why can't you use EXIT then? It terminates the cmd process. What else should happen?

Regards
aGerman

allal
Posts: 34
Joined: 04 Jun 2011 05:49

Re: preventing the call return

#5 Post by allal » 16 Jul 2011 14:49

aGerman wrote:Hmm, why can't you use EXIT then? It terminates the cmd process. What else should happen?


i don't want to exit as the error will not be shown to the user further more when the error is printed it must be printed in red color

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: preventing the call return

#6 Post by Ed Dyreen » 16 Jul 2011 15:10

'
An error is always shown on console :shock:
the error will not be shown to the user

Code: Select all

echo.this is ok
>con echo.this can't be supressed
>&2 echo.this goes to channel 2
>2&1 echo.this goes to channel 2 and channel 1
( >&2 echo.channel 2 is suppressed ) 2>nul

it must be printed in red color

Code: Select all

color 0C

Code: Select all

if not errorlevel 1 goto :skip
rem throw a terminating error to prevent returning
echo.no such file &pause
rem a happy crash to prevent returning
for %%! in ( %%~ )
:skip

allal
Posts: 34
Joined: 04 Jun 2011 05:49

Re: [Solved] preventing the call return

#7 Post by allal » 17 Jul 2011 04:12

Ed Dyreen wrote:'
An error is always shown on console :shock:

but this will continue after the error returning to the call statement

Code: Select all

rem a happy crash to prevent returning
for %%! in ( %%~ )


this code really disallow return to call statement but an accidental press will
exit the console,furthermore it is like an emulation of the exit statement.

i have discovered the solution and it was very damn simple.
sometimes when something is very easy only beginners can solve it

Code: Select all

@echo off
if not exist c:\file.txt call :ThrowException No Such A File :: c:\file.txt

:ThrowException
cmd /T:0C /K echo.%*

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: [Solved] preventing the call return

#8 Post by Ed Dyreen » 17 Jul 2011 05:25

'
Glad you solved it, guess we both had problems figuring what you were after :roll:
furthermore it is like an emulation of the exit statement.
Maybe, but technically a crash is a crash, and an exit is an exit

Code: Select all

call :a
echo.terminate process
exit %errorlevel%

:a
call :b
echo.terminate session
exit /b

:b
echo.crash process
for %%! in ( %%~ )

allal
Posts: 34
Joined: 04 Jun 2011 05:49

Re: [Solved] preventing the call return

#9 Post by allal » 17 Jul 2011 09:21

Ed Dyreen wrote:'
Maybe, but technically a crash is a crash, and an exit is an exit


i tested you code in the prompt and it didn't exit the prompt
and when i tested it in the batch file it prevents from returning to the call statement and it exit the batch file

i tried to cause a crash with '????' is not recognized as an internal command...
and it failed

Code: Select all

for %%! in ( %%~ )

what a strange code !!!

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: [Solved] preventing the call return

#10 Post by dbenham » 17 Jul 2011 12:14

allal wrote:i have discovered the solution and it was very damn simple.
sometimes when something is very easy only beginners can solve it

Code: Select all

@echo off
if not exist c:\file.txt call :ThrowException No Such A File :: c:\file.txt

:ThrowException
cmd /T:0C /K echo.%*

Nothing has been solved :? :!: Type EXIT into the console after the pseudo "exception" and the script continues execution.

caller.cmd

Code: Select all

@echo off
call script.cmd
echo MISSION FAILED!
exit /b

script.cmd

Code: Select all


@echo off
if not exist c:\file.txt call :ThrowException No Such A File :: c:\file.txt
echo Next line in script if no error
::Normally exit script before subroutines are defined
exit /b

:ThrowException
cmd /T:0C /K echo.%*

Demonstration of failure - Interactive session with prompt set to > without showing the current path:
>caller.cmd
No Such A File :: c:\file.txt

>exit
Next line in script if no error
MISSION FAILED!

>


Regarding Ed's code:
allal wrote:

Code: Select all

for %%! in ( %%~ )

what a strange code !!!

Yes - Ed has "solved" your problem by intentionally introducing a fatal syntax error. The code can be made even simpler:

Code: Select all

for


The ugly syntax error message can be eliminated by redirecting stderr to nul. But there is still a potential problem - any SETLOCAL will still be in effect after the script terminates :!:

caller2.cmd

Code: Select all

@echo off
call script2
echo MISSION FAILED!
exit /b

script2.cmd

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set test=SETLOCAL is still in effect

if not exist c:\file.txt call :ThrowException No Such A File :: c:\file.txt
echo Next line in script if no error
exit /b

:ThrowException
echo.%*
2>nul call :FatalSyntaxError

:FatalSyntaxError
for

Interactive Session:
>set test
Environment variable test not defined

>echo !test!
!test!

>caller2
No Such A File :: c:\file.txt

>echo !test!
SETLOCAL is still in effect

>

You can type EXIT at this point and the batch script(s) will NOT resume (the console will terminate). But you can see that SETLOCAL ENABLEDELAYEDEXPANSION is still in effect. You need to be careful in your subsequent processing :!:

I talk about persistence of SETLOCAL after script termination in this thread.

Dave Benham

allal
Posts: 34
Joined: 04 Jun 2011 05:49

Re: [Solved] preventing the call return

#11 Post by allal » 17 Jul 2011 12:54

ok you really saved me benham thanks a lot

so i will stick with the fatal syntax error

Post Reply