批处理文件 - 错误处理 [英] Batch Files - Error Handling
问题描述
我目前正在写我的第一个批处理文件部署一个asp.net的解决方案。
我一直在谷歌上搜索了一下对于一般的错误处理方法,并不能发现什么真正有用的。
I'm currently writing my first batch file for deploying an asp.net solution. I've been Googling a bit for a general error handling approach and can't find anything really useful.
基本上,如果任何事情不顺心我想停下来,并打印出了什么毛病。
Basically if any thing goes wrong I want to stop and print out what went wrong.
谁能给我任何指针?
推荐答案
我通常发现的条件命令连接操作比ERRORLEVEL方便多了。
I generally find the conditional command concatenation operators much more convenient than ERRORLEVEL.
yourCommand && (
echo yourCommand was successful
) || (
echo yourCommand failed
)
有一个复杂,你应该知道的。如果成功的分支中的最后一个命令引发错误的错误分支将闪光。
There is one complication you should be aware of. The error branch will fire if the last command in the success branch raises an error.
yourCommand && (
someCommandThatMayFail
) || (
echo This will fire if yourCommand or someCommandThatMayFail raises an error
)
解决方法是插入保证在成功分支的末端成功无害的命令。我喜欢用(呼叫)
,它不为0,除了没有设置ERRORLEVEL有一个推论(呼叫)
什么也不做,除了ERRORLEVEL设置成1。
The fix is to insert a harmless command that is guaranteed to succeed at the end of the success branch. I like to use (call )
, which does nothing except set the ERRORLEVEL to 0. There is a corollary (call)
that does nothing except set the ERRORLEVEL to 1.
yourCommand && (
someCommandThatMayFail
(call )
) || (
echo This can only fire if yourCommand raises an error
)
请参阅万无一失的方法在Windows批处理文件检查非零(错误)返回code代表的例子使用复杂ERRORLEVEL检测到错误,当需要的。
See Foolproof way to check for nonzero (error) return code in windows batch file for examples of the intricacies needed when using ERRORLEVEL to detect errors.
这篇关于批处理文件 - 错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!