1

Many things have be mentioned for nested exception handling in Delphi or fpc. Something like this for example. My question, that maybe solves the need for nested try... blocks, is if there is an actual difference between the following 2 versions of code, I don't see any except if an undefined behavior or something occurs after expect or finally...

try
    StrToInt('AA');
finally
    writeln('I absolutely need this');
end;
writeln('and this');

and...

try
  StrToInt('AA');
except
end;
writeln('I absolutely need this');
writeln('and this');
Community
  • 1
  • 1
Vassilis
  • 2,878
  • 1
  • 28
  • 43

1 Answers1

8

Yes there is a difference. Huge one.

If there is no exception in try block then both versions will execute all code, but if there is an exception behavior differs.

In first version of your code, anything after finally block will not be executed and exception will be propagated to the next level.

try
    StrToInt('AA'); // if this code throws exception and it will
finally
    writeln('I absolutely need this'); // this line will execute
end;
writeln('and this'); // this line will not execute 

In second version exception will be handled by except block and code following will continue with normal execution.

try
  StrToInt('AA'); // if this code throws exception and it will
except
end;
writeln('I absolutely need this'); // this line will execute
writeln('and this'); // this line will also execute

In linked question you have nested exception blocks, and that situation will behave differently than above one, in the way it was explained in answers to that question.


Documentation: Delphi Exceptions

Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159
  • Haven't seen that in documentation. So, in a `try..finally` block, the control jumps out of the function when `finally` ends, **if* and only *if* an exception is raised during `try`. Thanks for clearing this. – Vassilis Apr 16 '16 at 19:10
  • You mean this? "If an exception is raised but not handled in the finally clause, **that exception** is propagated out of the try...finally statement...". I hope this discussion is not a result of my bad English! – Vassilis Apr 16 '16 at 19:27
  • *" ... once statementList2 finishes executing, the exception is re-raised. "* – Sertac Akyuz Apr 16 '16 at 19:29
  • 3
    Reading and understanding are two different things. Especially, if English is not your native language. Obvious things may be not so obvious. – Dalija Prasnikar Apr 16 '16 at 19:34
  • 3
    IRL try...finally is mostly used to manage scoped allocations and has nothing to do with EH. A funny fact is that for your example, variants of StrtoInt exist: TryStrToInt, StrToIntDef, etc. – Abstract type Apr 17 '16 at 12:25
  • 3
    FWIW, in the second code block, the exception is not handled, just swallowed. What should IMO be stressed is that try-finally is used for resource protection, while try-except is really meant to handle the exceptions one can handle (and not just swallow them). – Rudy Velthuis Apr 17 '16 at 23:07