【发布时间】:2018-07-29 08:13:39
【问题描述】:
我遇到了一个奇怪的错误,我不知道是什么原因造成的 我有一个非常简单的控制台应用程序,带有 try-catch
static int Main()
{
int returncode = 0;
try
{
//some processing
}
catch
{
returncode = 1;
}
return returncode;
}
还有一个批处理文件
echo off
theprogram.exe
if errorlevel 1 goto Error
if errorlevel 0 goto Sucesss
echo I dont know what happened
echo %errorlevel%
goto :EOF
:Error
echo There was an error
goto :EOF
:Sucess
echo Completed Succesfully
goto :EOF
当我成功执行批处理文件时,它运行良好。 但是当我在失败的情况下执行它时,即使返回值为 1 我有时得到 255,有时得到 -532462766...
我不知道为什么...(我不习惯使用批处理文件)
编辑: 我做了更多的实验。该代码使用 DLL。如果该 DLL 不存在,则会发生上述情况。 我猜测当 DLL 不存在时,它会抛出一个异常,该异常将被 catch 捕获,因此返回代码为 1,但似乎不会发生
更奇怪的是,如果我故意抛出“DLLNotFoundException”,它会很好用
try
{
throw new DllNotFoundException();
}
catch
{
returncode = 1;
}
但在实际 DLL 不存在时不会
【问题讨论】:
标签: c# batch-file dll return-value