Delphi: Which exception class should be used to raise last Win32/Win64 errors?
When writing a function/procedure/method that utilizes Windows API functions or functions of certain Windows DLLs (eg ntdll.dll), which Delphi exception class should be used to raise the last Win32/Win64 errors?
Quoted from Delphi XE3 documentation about System.SysUtils.EExternal:
"Note:EExternal
classes represent Win32 exception codes. The ExceptionRecord
member points to a Win32 exception record." From that point, I conclude that there are differences between Win32 and Win64 exception mechanism. What are the differences between them?
For debugging purpose—I mean in regards to the stack frames related stuffs—, are the differences lead to different code implementation of exception handling for each platform?
First of all, I am assuming that your question concerns Delphi, despite the Free Pascal tag. I'm basing that assumption on the fact that you quote the Delphi documentation.
A Windows API message should be converted into an exception by calling RaiseLastOSError
. This will raise an EOSError
. That is a native Delphi exception.
The EExternalError
exception is unrelated. That's what is used when the RTL converts a system trap, eg access violation, maths errors etc. into a native exception. Note that Win32 is commonly used to refer to both the 32 and 64 bit Windows API. There's really only one interface with both 32 and 64 bit variants.
The underlying exception handling model is completely different between 32 and 64 bit Windows. The 32 bit model is stack based and the 64 bit model is table based. This means that the implementations of exception handling, and try/finally, are completely different between 32 and 64 bit architectures.
The original implementation in XE2 of the 64 bit table based model had a large number of faults. I am pleased to say that, following a number of QC reports submitted by myself and others, the implementation in XE3 is much improved.
Stack Overflow is not the place to go into the low-level details of the exception handling ABI of these two architectures. Instead I offer the following articles: