Exception is represented by an object (class instance) in most modern high-level programming languages. This means that exceptions can be inherited from base classes, as well as be extended with arbitrary properties.
Hardware levelSince exception is a way to interrupt normal execution path of a code - it requires support from hardware level. Modern CPUs provides such support. However, user-mode applications do not have direct access to the hardware. Therefore, operating system provides method to use exceptions on particular hardware. This is called SEH ("Structured Exception Handling") in Windows.
OS levelException on operating system level is represented by its address, code, options ("flags") and up to 15 integers ("params").
_EXCEPTION_RECORD = record
Basically, it is a simple record. It contains only simple data types. Therefore, any programming language is able to understand it. E.g. exceptions on operating system level can safely travel between executable modules (DLLs/exe).
Language levelHigh-level programming languages use SEH and the above low-level representation as basis for their own exception handling. For example, exception in high-level programming language (i.e. exception object) is implemented as OS exception with special code (for example: $EEECFADE for Delphi) and a pointer to object is stored in exception params. Exceptions with other codes are wrapped in generic class (EExternalException for Delphi). Pseudo-code:
var E: Exception; // Exception = class(TObject)
To properly handle such exceptions - programming language must know a "magic" code ($EEECFADE in the example above), as well as internal structure (memory layout) of Delphi objects. Naturally, a Microsoft's Visual Studio DLL does not know that.
Even various Delphi versions may be incompatible between each other. For example, a .Message property of exception object is an ANSI string in Delphi 2007 and earlier, and it is Unicode string in Delphi 2009 and later. Thus, Delphi 2007 will fail to process exception from Delphi 2009 and visa versa. In other words, if you are going to exchange language-specific exceptions between modules - both modules has to be compiled in the same version of the compiler.
ConclusionShort conclusion:
See also:
|