更新:似乎特定于D2007.它在D2010中工作,就像它在旧版本中工作一样.
我想返回一个退出代码,具体取决于Eception Handler块中捕获的异常类型,如:
program test;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
Exitcode: Integer;
begin
Writeln('Enter error code:');
Readln(Exitcode);
try
raise EExternal.Create('sdsdkfjh');
except
on E:EExternal do
begin
Writeln(E.Classname, ': ', E.Message);
Halt(Exitcode);
end;
end;
end.
Run Code Online (Sandbox Code Playgroud)
不幸的是,在D2007中,从异常块调用Halt(n)总是返回退出代码1,无论你传递给Halt().
显然,因为退出Exception处理程序调用Finalize,它会清除挂起(非Abort)异常,调用SysUtils.ExceptHandler:
procedure ExceptHandler(ExceptObject: TObject; ExceptAddr: Pointer); far;
begin
ShowException(ExceptObject, ExceptAddr);
Halt(1); // <= @#$##@#$!
end;
Run Code Online (Sandbox Code Playgroud)
无论我想要什么退出代码,我都能得到它Halt(1)!
所以问题是:
我怎样才能简单地返回所需的退出代码,具体取决于引发的异常?
我有一个主要功能,可以完成很多IO.但是,有一点,我想检查一个变量,例如not (null shouldBeNull)退出整个程序,而不继续,使用linux exitcode 1并输出错误消息.
我试过玩,error "..."就像把它放在if:
if (not (null shouldBeNull)) error "something bad happened" else putStrLn "ok"
但我得到了parse error (possibly incorrect indentation):(.
这是一个改变的片段.
main :: IO ExitCode
main = do
--Get the file name using program argument
args <- getArgs
file <- readFile (args !! 0)
putStrLn("\n")
-- ... (some other io)
-- [DO A CHECK HERE], exit according to check..
-- ... (even more io)
echotry <- system "echo success" …Run Code Online (Sandbox Code Playgroud) 我有以下exec任务,执行assemblyinfo.cs文件的签入.我正在尝试返回退出代码,但由于某种原因它总是空的.
<!--Checkin if all succeeded-->
<Exec Condition=" '$(LocalCompilationSuccess)' != 'Failed' and '$(LocalTestSuccess)' != 'Failed' " ContinueOnError="True"
Command='"$(TfCommand)" checkin /recursive /comment:"$(NoCICheckInComment) $(BuildDefinitionName): build succeeded, checkin changes." /override:"TeamBuild $(BuildDefinitionName)" $/SomeProject/Trnk' WorkingDirectory="$(SolutionRoot)" >
<Output TaskParameter="ExitCode" PropertyName="ErrorCode"/>
</Exec>
Run Code Online (Sandbox Code Playgroud)
我试图以两种方式阅读退出代码:
'%(ErrorCode.Identity)'
'$(ErrorCode)'
Run Code Online (Sandbox Code Playgroud)
两者都是空的.有什么建议?
在Java中,我们使用System.exit(int)退出程序.C中"退出值"的原因是退出值用于检查程序中的错误.但是在Java中,错误会被抛出异常反映出来,因此可以轻松处理它们.那么为什么我们在Java中有退出值呢?
我想在我的Bash脚本中进行清理操作,如下所示:
#! /bin/bash
set -eu
trap 'echo "E: failed with exitcode $?" 1>&2' ERR
true
false
Run Code Online (Sandbox Code Playgroud)
使用$?脑海作为一种自然选择,但事实并非如此.它总是包含0.有什么方法可以"窥探" ERR陷阱中的exitcode 吗?
[更新:]我不知道我之前测试过的是什么.这段代码就像一个魅力,所以我把它留在这里作为一个小而好的例子.
我正在写一个测试,看看我是否可以可靠地确定退出代码的整数值wait.
1.为什么退出代码乘以256?
2.是exit(),wait()操作系统还是做其他事情的其他东西?
重现问题的代码.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
// implementation is correct but irrelevant to the question
int compareInt(const void* a, const void* b);
int main(void) {
pid_t pids[6];
int i;
for (i = 0; i < 6; i++) {
pid_t pid = fork();
pids[i] = pid;
if (pid == 0) {
exit(i);
}
}
int codes[6];
do {
i--;
wait(&codes[i]);
} while (i > …Run Code Online (Sandbox Code Playgroud) 在 shell 脚本中,我使用带有大括号的代码块将所有输出通过管道传输到控制台和带有 tee 的日志文件。
#!/bin/bash
{
echo "Foo bar"
echo "foo bar on STDERR" >&2
ERRL=66
exit 99
} 2>&1 | tee log-file.log
Run Code Online (Sandbox Code Playgroud)
(这只是一个小演示脚本,原来的要复杂得多)
问题是,该行exit 99没有效果,脚本以退出代码 0 结束。(我认为这是 tee 命令的退出代码)
exit $ERRL我尝试在脚本末尾添加该行,但它显示变量 $ERRL 在大括号之外为空。
当代码块内出现问题时,我能做什么以错误代码结束脚本——而不丢失日志文件的输出?
生成文件:
\n\nfoo.o: foo.cpp\n g++ -o foo.o foo.cpp\nRun Code Online (Sandbox Code Playgroud)\n\nfoo.cpp:
\n\naslkfdj\nRun Code Online (Sandbox Code Playgroud)\n\n输出:
\n\n$ g++ -o foo.o foo.cpp; echo $?\nfoo.cpp:1:1: error: \xe2\x80\x98aslkfdj\xe2\x80\x99 does not name a type\n aslkfdj\n ^\n1\n\n$ make; echo $?\ng++ -o foo.o foo.cpp\nfoo.cpp:1:1: error: \xe2\x80\x98aslkfdj\xe2\x80\x99 does not name a type\n aslkfdj\n ^\nMakefile:2: recipe for target 'foo.o' failed\nmake: *** [foo.o] Error 1\n0\n$ \nRun Code Online (Sandbox Code Playgroud)\n\n我期望 make 的退出代码为 2,因为 g++ 返回非零,参见。
\n\n$ man make|grep exits -A2\n GNU make exits with a status of zero if all makefiles …Run Code Online (Sandbox Code Playgroud) 我正在为Windows任务调度程序编写一个控制台程序来运行.我的Main()方法有一个返回类型,int并在退出时返回不同的数字以指示执行的结果,我可以在.BAT脚本中访问它%errorlevel%.
但是在VS2015中进行调试时,我会做一个
return 255;
Run Code Online (Sandbox Code Playgroud)
我总是从VS2015的输出窗口获取:
The program '[43560] Foo.vshost.exe' has exited with code 0 (0x0).
Run Code Online (Sandbox Code Playgroud)
现在,如果我希望"输出"窗口显示我的程序的退出代码,我必须Application.Exit(255)为其显示
The program '[24400] Foo.vshost.exe' has exited with code 255 (0xff).
Run Code Online (Sandbox Code Playgroud)
奇怪的是,%errorlevel%如果我CMD.exe使用return语句运行程序,则正确设置为255 Environment.Exit().
所以我的问题是
返回值Main()有些不同Environment.ExitCode吗?
Main()在VS2015中轻松找出方法返回值的方法是什么?
退出控制台程序时,是否Environment.Exit()比简单的返回语句更受欢迎?因为回报声明更符合我的口味.
有人能告诉我这背后的故事吗?谢谢.
我们将valgrind其用作 CI 流程的一部分。如果存在一些内存问题,valgrind必须返回非零代码,并且报告此事件。这就是我们运行它的方式:
valgrind --error-exitcode=1 --tool=memcheck --leak-check=full \
--errors-for-leak-kinds=definite --show-leak-kinds=definite \
--track-origins=yes ./some_cgo_application
(...)
==25182== HEAP SUMMARY:
==25182== in use at exit: 2,416,970 bytes in 34,296 blocks
==25182== total heap usage: 83,979 allocs, 49,684 frees, 5,168,335 bytes allocated
==25182==
==25182== LEAK SUMMARY:
==25182== definitely lost: 0 bytes in 0 blocks
==25182== indirectly lost: 0 bytes in 0 blocks
==25182== possibly lost: 3,024 bytes in 7 blocks
==25182== still reachable: 2,413,946 bytes in 34,289 blocks
==25182== of …Run Code Online (Sandbox Code Playgroud) exit-code ×10
bash ×2
shell ×2
bash-trap ×1
c ×1
c# ×1
delphi ×1
delphi-2007 ×1
exception ×1
exec ×1
exit ×1
halt ×1
haskell ×1
io ×1
java ×1
main-method ×1
makefile ×1
memory-leaks ×1
monads ×1
msbuild ×1
php ×1
return-value ×1
scripting ×1
ubuntu-14.04 ×1
valgrind ×1