简体   繁体   English

代码如何在异常后执行?

[英]How is code executing after an exception?

I must be missing something... How can an exception be thrown, yet the code following the exception still gets hit in the debugger? 我必须遗漏一些东西......如何抛出异常,但异常后的代码仍会在调试器中被击中?

private UpdaterManifest GetUpdaterManifest()
{
    string filePathAndName = Path.Combine(this._sourceBinaryPath, this._appName + ".UpdaterManifest");

    if (!File.Exists(filePathAndName))
    {
        // This line of code gets executed:
        throw new FileNotFoundException("The updater manifest file was not found. This file is necessary for the program to run.", filePathAndName);
    }

    UpdaterManifest updaterManifest;

    using (FileStream fileStream = new FileStream(filePathAndName, FileMode.Open))
    {
        // ... so how is it that the debugger stops here and the call stack shows
        // this line of code as the current line? How can we throw an exception
        // above and still get here?
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(UpdaterManifest));
        updaterManifest = xmlSerializer.Deserialize(fileStream) as UpdaterManifest;
    }

    return updaterManifest;
}

Some scenario's where this can generally happen: 某些情况通常会发生这种情况:

  • when the option "Require source files to exactly match the original version" switched off. 当关闭“要求源文件与原始版本完全匹配”选项时。 In that case, you don't get a warning when your files are out of sync. 在这种情况下,当文件不同步时,您不会收到警告。

  • when the IDE asks for "There were build errors. Would you like to continue and run the last successful build?" 当IDE要求“存在构建错误时。你想继续并运行最后一次成功的构建吗?” , in which case the IDE can be wrong about the correct line, because it runs an earlier version. ,在这种情况下IDE可能是错误的正确的行,因为它运行早期版本。

  • when you are debugging a release version of your code, where certain parts are optimized away. 当您调试代码的发布版本时,某些部分会被优化掉。 This results in the highlighted line to be the next available line in the source that reflects and actual statement in the optimized code (this you'll often see when debugging with external assemblies that are optimized). 这导致突出显示的行成为源中的下一个可用行,它反映了优化代码中的实际语句(在使用优化的外部程序集进行调试时,您经常会看到这一行)。


EDIT: I kind-of misread your code. 编辑:我有点误读你的代码。 Between the "throw" and the line that gets highlighted, there's only a declaration of a variable, no code at all to be executed. 在“throw”和突出显示的行之间,只有一个变量的声明,根本没有代码可以执行。 I assume that you meant that the code "using..." was highlighted? 我假设您的意思是“使用...”代码突出显示了? Because that's as expected: it is the first line after the throw-statement (the throw-statement itself doesn't "catch" the error for the debugger). 因为这是预期的:它是throw语句之后的第一行(throw语句本身没有“捕获”调试器的错误)。

See screenshot: 看截图: 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM