How is code executed after an exception?

I need to miss something ... How can an exception be thrown, but the code following the exception still gets into 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; } 
+6
source share
1 answer

Some scenarios in which this usually occurs:

  • when the option "Require source files to match the original version" is disabled. In this case, you will not receive a warning when your files are not synchronized.

  • when the IDE asks, "There were build errors. Would you like to continue and run the last successful build?", in which case the IDE may be mistaken in the correct line because it launches an earlier version.

  • when debugging a version of your code version where some parts are optimized. This leads to the fact that the selected line will be the next available line in the source, which also reflects the actual instruction in the optimized code (this is often seen when debugging with optimized external assemblies).


EDIT: I seem to be reading your code incorrectly. Between the throw and the line highlighted, there is only a variable declaration; no code should be executed at all. I assume that you meant that the code "using ..." was highlighted? Because this is as expected: this is the first line after the throw statement (the throw statement itself will not "catch" the error for the debugger).

See screenshot: enter image description here

+5
source

Source: https://habr.com/ru/post/904089/


All Articles