批处理文件尝试捕获 [英] Batch File try catch

查看:66
本文介绍了批处理文件尝试捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理脚本,其语句如下所示:

I have a batch script with the statement as shown below:

for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set TIME=%%j
set TIME=%TIME:~0,12%

但是一旦执行,它就会返回一个我首先无法理解的错误.然后才知道错误是由于缺少路径env变量中的 wbem .该bat文件位于中央存储库中,因此任何人都可以执行此操作.因此,需要让用户知道在本地计算机中再次发生此错误的原因.

But once I executed it returned an error which I couldnt understand first. Then got to know the error was because of missing the wbem in path env variable. This bat file is located in a central repository so that anyone can execute this. So, need to let the user know the reason if this error strikes again in their local machine.

我认为Batch中没有try catch语句.另外,还知道有关ErrorLevel的信息.但是我不确定如何实现.在这方面有人可以支持我吗.

I dont think there is a try catch statement in Batch. Also, known about ErrorLevel. But I am not sure how this can be implemented..Can anyone support me in this regard..

推荐答案

批处理文件处理错误的标准方法是使用

Standard method for batch files to handle errors is to use ERRORLEVEL variable. Zero means no errors, non-zero - error:

@rem some code
if %ERRORLEVEL% neq 0 goto ProcessError

@rem ... other code
exit /b 0

:ProcessError
@rem process error
exit /b 1

退出/b exitCode 将ERRORLEVEL设置为该exitCode.

exit /b exitCode sets ERRORLEVEL to this exitCode.

这篇关于批处理文件尝试捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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