如何在cmd或批处理文件中使用try,catch块? [英] How to use try, catch blocks in cmd or batch files ?

查看:2084
本文介绍了如何在cmd或批处理文件中使用try,catch块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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%  

推荐答案

原始批处理语言中没有任何东西可以接近异常处理。从来没有,在上发布了一个令人惊讶的复杂和强大的批处理异常处理实现http://www.dostips.com/forum/viewtopic.php?f=3&t=6497 ,它只使用纯粹的原生批次:-)



该技术允许使用以下形式定义TRY / CATCH块:

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"





异常冒泡直到检测到Try / Catch,然后完全处理异常并继续处理,否则重新抛出异常并且它继续冒泡,可能一直到命令l批处理终止的上下文。



按照顶部附近的链接获取完整代码,示例用法和解释。



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.


bat或cmd文件中没有这样的东西,甚至没有关闭。



如果你需要了解有关PowerShell结构化异常处理的更多信息,你可以找到很多许多地方的信息,例如: http://www.vexasoft.com/blogs/powershell/7255220-powershell-tutorial-try-catch-finally-and-error-handling-in-powershell [ ^ ]。



另外,请注意PowerShell是一种.NET语言,其结构化异常处理基于.NET并与之完全兼容。您可以使用嵌入在PowerShell中的某种.NET语言编写PowerShell类型,并在脚本中调用它们的方法。这些模块中抛出的异常可以在PowerShell脚本中捕获和处理,反之亦然。



另一种可以使用结构化异常处理的批处理脚本技术基于Javascript(和其他一些语言);您可以将它与Windows脚本宿主一起使用:

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 [ ^ ]。



实际上,实现了WSF引擎与其他语言的绑定,请参阅WSF的第一个链接;其中许多语言也允许您使用结构化异常处理。最后,您可以在PowerShell脚本中使用WSF。



-SA
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


这篇关于如何在cmd或批处理文件中使用try,catch块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆