Click here to Skip to main content
15,396,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
   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(){

ping asdfasdf
ping ghgsjhsjs
Write-Host "success"
exit 0

Write-Host "failure"
exit 1

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%  

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, and it uses nothing but pure, native batch :-)

The technique allows definition of TRY/CATCH blocks with the following form:
  REM Normal code goes here
  REM Exception handling code goes here

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.
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:[^].

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:[^],[^],[^].

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.


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