views:

200

answers:

5

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?

+1  A: 

It resumes where the exception is caught. Otherwise, what would be the point of writing the exception clause?

unutbu
One of the more esoteric features that the Perl6 people are thinking about are resumable exceptions. If I understand correctly, the exception handler would have an option to resume execution where the exception was thrown. Not sure if that is a good thing (as in: leading to code that people can still understand).
Thilo
I think that the throwing code would at least need to signal that like `throw [[returnable]] Type();`. Otherwise i imagine it could lead to a big mess.
Johannes Schaub - litb
Not everyone minds a big mess ;-)
Thilo
@~unutbu : Obviously :) I think that the real question was where does the execution resumes AFTER it had been caught.
Moshe Levi
"Warnings are produced in Perl 6 by throwing a resumable control exception to the outermost scope, which by default prints the warning and resumes the exception by extracting a resume continuation from the exception, which must be supplied by the warn() function (or equivalent). Exceptions are not resumable in Perl 6 unless the exception object does the Resumable role. " http://perlcabal.org/syn/S04.html
Thilo
@Moshe Levi, yes, I think you are right. I would delete my answer, if it would not also delete Thilo's interesting information regarding Perl 6.
unutbu
Perl 6 is flying against the hard-earned accumulated wisdom of a variety of languages where resumption has been tried. See the discussion in Stroustrup's "Design and Evolution of C++".
Jonathan Leffler
+5  A: 

The code inside the catch block is executed and the original execution continues right after the catch block.

Moshe Levi
You forgot the reference: http://docs.python.org/tutorial/errors.html and http://docs.python.org/c-api/exceptions.html
S.Lott
+1  A: 

Execution continues in the catch block (where the exception was caught).
This is consistent across languages that uses exceptions.

The important point to note (especially in C++)
Between the throw and the catch point the stack is unwound in an orderly manor so that all objects created on the stack are correctly destroyed (in the expected order). This has resulted in the technique knows as RAII.

Martin York
+3  A: 

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). in some languages, a finally block may also be executed after the catch. then the program proceed with the next instruction following the whole try ... 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 catch processes 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 every try .. 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: the finally does not exists in every language, some languages does not allow a finally and a catch in the same block (you have to nest two try to use the 2), some languages allows to catch everything (the catch (...) in C++) while some languages don't.

Adrien Plisson
+1  A: 

I don't have my copy of Bjarne Stroustrup's "Design & Evolution" handy, but I believe he wrote in there about some experience with resumable exceptions. They found that they made things considerably harder to get correct. After all, if an unexpected error happens in some line, your exception handler then has to patch the problem up sufficiently to allow execution to resume without knowing the context. This may be possible for an out-of-memory error (although such errors are frequently a result of runaway memory allocation, and adding some more memory won't really fix anything), but not for exceptions in general.

So, in C++ and all languages I'm familiar with, execution resumes with the catch, and doesn't automatically go back to the place that threw the exception.

David Thornley
I do have a copy of D this is not a matter of opinion but a matter of years of experience. Resumption is seductive, but not valid."
anon