Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 452
Bob Horn Avatar asked Dec 19 '11 15:12

Bob Horn


People also ask

Does the code execute after an exception is thrown?

When an exception is encountered, execution stops and the exception is propagated up the call stack until the appropriate handler can handle it (this may be a catch block that corresponds to the try that wraps the statement in question within the same method, or it may be a catch block further up the call-stack.

What happens after catching an exception?

If exception occurs in try block then the rest of the statements inside try block are ignored and the corresponding catch block executes. After catch block, the finally block executes and then the rest of the program.

Where does execution resume after an exception has been thrown and caught?

the execution resumes where the exception is caught, that is at the beginning of the catch block which specifically address the current exception type. the catch block is executed, the other catch blocks are ignored (think of multiple catch block as a switch statement).

What to do after throwing an exception?

After throwing an exception, you do not need to return because throw returns for you. Throwing will bubble up the call stack to the next exception handler so returning is not required.


1 Answers

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?", in which case the IDE can be wrong about the correct line, because it runs an earlier version.

  • 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. 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).

See screenshot: enter image description here

like image 95
Abel Avatar answered Oct 14 '22 22:10

Abel