if-语句总是采用正确的路径,除非我们将内部移动到它自己的方法
本文关键字:内部 我们将 非我 移动 方法 自己的 它自己 路径 语句 if- | 更新日期: 2023-09-27 18:31:30
我有一些代码可以获取Checkpoint
对象,我们检查它是否有值,如果没有,则抛出异常。 但是,无论版本中的任何值,它总是引发异常。
if (!mostRecentCheckpoint.Version.HasValue)
{
throw new ApplicationException(
string.Format("Invalid version of {0} Checkpoint not found for the case {1}",
CaseProductDataCheckpointType.CurrentActive, caseId));
}
但是,如果我们用方法替换异常,它会做正确的事情:
if (!mostRecentCheckpoint.Version.HasValue)
{
ThrowException(caseId);
}
...
void ThrowException(int caseId)
{
throw new ApplicationException( etc...)
}
这很好,因为我们已经解决了这个问题,但我想知道它为什么这样做。 我不知道它是否相关,但是此代码在IIS中运行 - 代码的单元测试运行正确。
原来出现此问题是由于 VS2010 调试器中的问题。
http://whileicompile.wordpress.com/2010/07/02/visual-studio-bug-if-followed-by-a-try-catch-causes-debugger-stepping-error/
我会把它留在这里,以防其他人有同样的问题。