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:what a strange code !!!
Yes - Ed has "solved" your problem by intentionally introducing a fatal syntax error. The code can be made even simpler:
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