Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
CSS
Hi,
   I want to use try, catch blocks in cmd or batch files .Please suggest how to do it?
Suppose I have 10 commands in a batch file.If any error occurs in 2nd or 3rd command then I need to capture that error.In cmd,we have %errorlevel% .But it captures only last executed run exit code.i.e %errorlevel% returns only 10th command exit code.But I want to get 2nd or 3rd exit code if they fails.

%errorlevel% returns 0 for success.

In powershell ,I was using below code

function a(){
try
{

ping asdfasdf
ping 171.21.000.145
ping ghgsjhsjs
Write-Host "success"
exit 0
}
catch
{

Write-Host "failure"
exit 1
}
}
a


In above code, asdfasdf and ghgsjhsjs are wrong server names.So I want to go to catch block if first ping command fails.How to do this in batch file.Atleast suggest me the alternative to try and catch block in batch file instead of %errorlevel%  
Posted

It is true there is nothing built into the primitive batch language that even comes close to exception handling. Never-the-less, there is a surprisingly sophisticated and robust implementation of batch exception handling posted at http://www.dostips.com/forum/viewtopic.php?f=3&t=6497, and it uses nothing but pure, native batch :-)

The technique allows definition of TRY/CATCH blocks with the following form:
:someRoutine
setlocal
%@Try%
  REM Normal code goes here
%@EndTry%
:@Catch
  REM Exception handling code goes here
:@EndCatch


And exceptions can be thrown with
call exception throw -156 "Exception message" "LocationString"


Exceptions bubble up until Try/Catch is detected, whereupon the exception is either handled completely and processing continues, or else an exception is rethrown and it continues to bubble up, possibly all the way to the command line context where batch processing is terminated.

Follow the link near the top for full code, example usage, and explanation.
 
Share this answer
 
There is no such things in bat or cmd files, not even close.

If you need to learn more on PowerShell structured exception handling, you can find a lot of information in many places, for example: http://www.vexasoft.com/blogs/powershell/7255220-powershell-tutorial-try-catch-finally-and-error-handling-in-powershell[^].

Also, note that PowerShell is a .NET language and its structured exception handling is based on .NET and is fully compatible with it. You can write PowerShell types in some .NET language embedded in PowerShell and call their method in the script. The exceptions thrown in such modules can be caught and handled in PowerShell script and visa versa.

Another batch scripting technology where you can use structured exception handling is based on Javascript (and some other languages); you can use it with Windows Script Host:
http://en.wikipedia.org/wiki/Windows_Script_Host[^],
http://msdn2.microsoft.com/en-us/library/9bbdkx3k.aspx[^],
http://msdn2.microsoft.com/en-us/library/98591fh7.aspx[^].

Actually, there are implementation of binding of the WSF engine with other languages, please see the first link on WSF; many of those languages also allow you to use structured exception handling. And, finally, you can use WSF in PowerShell scripts.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900