In general, where does program execution resume after an exception has been thrown and caught? Does it resume following the line of code where the exception was thrown, or does it resume following where it’s caught? Also, is this behavior consistent across most programming languages?
In general, where does program execution resume after an exception has been thrown and
Share
the execution resumes where the exception is caught, that is at the beginning of the
catchblock which specifically address the current exception type. the catch block is executed, the other catch blocks are ignored (think of multiplecatchblock as a switch statement). in some languages, afinallyblock may also be executed after thecatch. then the program proceed with the next instruction following the wholetry ... catch ... finally ....you should note that if an exception is not caught in a block, the exception is propagated to the caller of the current function, and up the call stack until a
catchprocesses the exception. in this case, you can think of function calls like a macro: insert the code of each function where it is called, and you will clearly see the nesting of everytry .. catch ... finally ...blocks.if there is no handler for an exception, the program generally crashes. (some languages may be different on this point).
the behavior for the execution flow is consistent accross every languages i know. the only difference lies in the
try ... catch ... finally ...construct: thefinallydoes not exists in every language, some languages does not allow afinallyand acatchin the same block (you have to nest twotryto use the 2), some languages allows to catch everything (thecatch (...)in C++) while some languages don’t.