Embarcadero Delphi Try-except nested inside try-finally


Example

A try-except block may be nested inside a try-finally block.

AcquireResource;
try
  UseResource1;
  try
    UseResource2;
  except
    on E: EResourceUsageError do begin
      HandleResourceErrors;
    end;
  end;
  UseResource3;
finally
  ReleaseResource;
end;

If an EResourceUsageError occurs in UseResource2, then execution will jump to the exception handler and call HandleResourceError. The exception will be considered handled, so execution will continue to UseResource3, and then ReleaseResource.

If an exception of any other type occurs in UseResource2, then the exception handler show here will not apply, so execution will jump over the UseResource3 call and go directly to the finally block, where ReleaseResource will be called. After that, execution will jump to the next applicable exception handler as the exception bubbles up the call stack.

If an exception occurs in any other call in the above example, then HandleResourceErrors will not be called. This is because none of the other calls occur inside the try block corresponding to that exception handler.